linux线程(一)基本应用
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
#include <stdio.h>
#include <unistd.h>
#include <pthread.h> void thread_function(void *param)
{
printf("this is a thread.\n");
}
int thread_test(void)
{
pthread_t thread_id;
int ret;
ret = pthread_create(&thread_id, NULL, (void *)&thread_function, NULL);
if(ret != ) {
printf("pthread_create fail\n");
return -;
} /*wait thread exit.*/
sleep();
return ;
}
int main(int argc, char *argv[])
{
thread_test();
return ;
}
此代码中存在一处内存泄漏问题,详情点击此处跳转。
线程退出函数原型:
void pthread_exit(void *retval);
int pthread_join(pthread_t thread, void **retval);
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
void thread_function(void *param)
{
printf("this is a thread.\n");
sleep();
pthread_exit((void *));
}
int thread_test(void)
{
pthread_t thread_id;
int ret;
int *retval;
ret = pthread_create(&thread_id, NULL, (void *)&thread_function, NULL);
if(ret != ) {
printf("pthread_create fail\n");
return -;
}
ret = pthread_join(thread_id, (void *)&retval);
if(ret != ) {
printf("pthread_join fail\n");
return -;
}
printf("thread return value is %d\n", retval);
}
int main(int argc, char *argv[])
{
thread_test();
return ;
}
int pthread_cancel(pthread_t thread);
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
pthread_t thread1_id, thread2_id;
void thread_function1(void *param)
{
printf("this is a thread 2.\n");
while() {
printf("thread 1 is running.\n");
sleep();
}
printf("thread 1 exit.\n");
}
void thread_function2(void *param)
{
printf("this is a thread 2.\n");
sleep();
printf("thread 2 cancel thread 1.\n");
pthread_cancel(thread1_id);
}
int thread_test(void)
{
int ret;
ret = pthread_create(&thread1_id, NULL, (void *)&thread_function1, NULL);
if(ret != ) {
printf("pthread_create fail\n");
return -;
}
ret = pthread_create(&thread1_id, NULL, (void *)&thread_function2, NULL);
if(ret != ) {
printf("pthread_create fail\n");
return -;
}
ret = pthread_join(thread1_id, NULL);
if(ret != ) {
printf("pthread_join fail\n");
return -;
}
}
int main(int argc, char *argv[])
{
thread_test();
return ;
}
只是简单的介绍了下线程的基本操作,更高级的应用稍后更新敬请期待~~
linux线程(一)基本应用的更多相关文章
- [转载]Linux 线程实现机制分析
本文转自http://www.ibm.com/developerworks/cn/linux/kernel/l-thread/ 支持原创.尊重原创,分享知识! 自从多线程编程的概念出现在 Linux ...
- linux线程的实现
首先从OS设计原理上阐明三种线程:内核线程.轻量级进程.用户线程 内核线程 内核线程就是内核的分身,一个分身可以处理一件特定事情.这在处理异步事件如异步IO时特别有用.内核线程的使用是廉价的,唯一使用 ...
- linux线程的实现【转】
转自:http://www.cnblogs.com/zhaoyl/p/3620204.html 首先从OS设计原理上阐明三种线程:内核线程.轻量级进程.用户线程 内核线程 内核线程就是内核的分身,一个 ...
- Linux线程-创建
Linux的线程实现是在内核以外来实现的,内核本身并不提供线程创建.但是内核为提供线程[也就是轻量级进程]提供了两个系统调用__clone()和fork (),这两个系统调用都为准备一些参数,最终都用 ...
- Linux线程学习(一)
一.Linux进程与线程概述 进程与线程 为什么对于大多数合作性任务,多线程比多个独立的进程更优越呢?这是因为,线程共享相同的内存空间.不同的线程可以存取内存中的同一个变量.所以,程序中的所有线程都可 ...
- Linux线程学习(二)
线程基础 进程 系统中程序执行和资源分配的基本单位 每个进程有自己的数据段.代码段和堆栈段 在进行切换时需要有比较复杂的上下文切换 线程 减少处理机的空转时间,支持多处理器以及减少上下文切换开销, ...
- Linux 线程(进程)数限制分析
1.问题来源公司线上环境出现MQ不能接受消息的异常,运维和开发人员临时切换另一台服务器的MQ后恢复.同时运维人员反馈在出现问题的服务器上很多基本的命令都不能运行,出现如下错误:2. 初步原因分析和 ...
- Linux 线程与进程,以及通信
http://blog.chinaunix.net/uid-25324849-id-3110075.html 部分转自:http://blog.chinaunix.net/uid-20620288-i ...
- linux 线程的内核栈是独立的还是共享父进程的?
需要考证 考证结果: 其内核栈是独立的 206 static struct task_struct *dup_task_struct(struct task_struct *orig) 207 { 2 ...
- Linux 线程模型的比较:LinuxThreads 和 NPTL
Linux 线程模型的比较:LinuxThreads 和 NPTL GNU_LIBPTHREAD_VERSION 宏 大部分现代 Linux 发行版都预装了 LinuxThreads 和 NPTL,因 ...
随机推荐
- oracle学习----逻辑读
1.物理读 当数据块第一次读取到,就会缓存到buffer cache 中,而第二次读取和修改该数据块时就在内存buffer cache 清空数据缓冲区 SQL> alter session se ...
- Android(java)学习笔记161:Framework运行环境之启动SystemServer进程
SystemServer进程是zygote孵化出的第一个进程,该进程是从ZygoteInit.java的main函数中调用startSystemServer()开始的.与启动普通进程的差别 ...
- MySQL之DML语句(insert update delete)
DML主要针对数据库表对象的数据而言的,一般DML完成: 插入新数据 修改已添加的数据 删除不需要的数据 1.insert into插入语句 //主键自增可以不插入,所以用null代替 ); //指定 ...
- 20151225jquery学习笔记---编辑器插件
编辑器(Editor),一般用于类似于 word 一样的文本编辑器,只不过是编辑为 HTML格式的.分类纯 JS 类型的,还有 jQuery 插件类型的.一. 编辑器简介我们使用的 jQuery 版本 ...
- C10K问题和Libevent库介绍
http://blog.chinaunix.net/uid-20761674-id-75056.html 一.C10K的问题 C10K的问题在上个世纪90年代就被提出来了.大概的意思是当用户数超过1万 ...
- oracle数据库TNS
TNS是Oracle Net的一部分,专门用来管理和配置Oracle数据库和客户端连接的一个工具,在大多数情况下客户端和数据库要通讯,必须配置TNS,下面看一如何配置它吧: TNS简要介绍与应用 :O ...
- WKWebView无法(通过URL schemes)跳转到其他App
Custom scheme URL 在WKWebView中默认是不支持的 (但Safari可以). 我们可以通过NSError来进行一些处理从而使得程序可以正常跳转: func webView(web ...
- swift-08-使用键值对儿统计字符在字符串中出现的次数
// // main.swift // 12- // // Created by wanghy on 15/8/9. // Copyright (c) 2015年 wanghy. All ri ...
- java 利用注解实现BaseDao 增删查改
第一步,编写两个注解类,用于表明实体类对应的表名及字段. TableInfo.java 此注解用于标注表名及主键名 import static java.lang.annotation.Element ...
- 06_WebService与Socket的区别
[区别] 区别1. Socket是基于TCP/IP的传输层协议. WebService是基于HTTP协议传输数据的,HTTP是基于TCP的应用层协议. 区别2. WebService采用了基于HTTP ...