linux多线程编程详解
来源:互联网 发布:我与网络强国演讲稿 编辑:程序博客网 时间:2024/05/22 06:14
注:如果unubtu下 man手册不全,需要安装 apt-get install manpages-posix-dev
一、线程创建
PTHREAD_CREATE(3) Linux Programmer's Manual PTHREAD_CREATE(3)NAME pthread_create - create a new threadSYNOPSIS #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); Compile and link with -pthread.返回值:若成功,返回0;否则,返回错误编码
参数:第二参数attr是线程的属性,可以设置为NULL使用默认属性
二、获取线程ID
PTHREAD_SELF(3) Linux Programmer's Manual PTHREAD_SELF(3)NAME pthread_self - obtain ID of the calling threadSYNOPSIS #include <pthread.h> pthread_t pthread_self(void); Compile and link with -pthread.返回值:调用线程的线程ID
三、线程ID比较
PTHREAD_EQUAL(3) Linux Programmer's Manual PTHREAD_EQUAL(3)NAME pthread_equal - compare thread IDsSYNOPSIS #include <pthread.h> int pthread_equal(pthread_t t1, pthread_t t2); Compile and link with -pthread.返回值:若相等,返回非0数值;否则,返回0
四、线程终止
PTHREAD_EXIT(3) Linux Programmer's Manual PTHREAD_EXIT(3)NAME pthread_exit - terminate calling threadSYNOPSIS #include <pthread.h> void pthread_exit(void *retval); Compile and link with -pthread.retval参数是一个无类型的指针,进程中的其他线程也可以通过调用pthread_join访问到这个指针
五、阻塞等待指定线程退出
PTHREAD_JOIN(3) Linux Programmer's Manual PTHREAD_JOIN(3)NAME pthread_join - join with a terminated threadSYNOPSIS #include <pthread.h> int pthread_join(pthread_t thread, void **retval); Compile and link with -pthread.返回值:若成功,返回0;否则,返回错误编码
注:如果线程已经处于分离状态,pthread_join就会处于调用失败。
六、取消同一进程中的其他线程
PTHREAD_CANCEL(3) Linux Programmer's Manual PTHREAD_CANCEL(3)NAME pthread_cancel - send a cancellation request to a threadSYNOPSIS #include <pthread.h> int pthread_cancel(pthread_t thread); Compile and link with -pthread.返回值:若成功,返回0;否则,返回错误编码
注:线程可以选择忽略取消或者控制如何被取消。
七、分离线程函数
PTHREAD_DETACH(3) Linux Programmer's Manual PTHREAD_DETACH(3)NAME pthread_detach - detach a threadSYNOPSIS #include <pthread.h> int pthread_detach(pthread_t thread); Compile and link with -pthread.返回值:若成功,返回0;否则,返回错误编码
一个比较完整的例子,用到了上面的7个函数:gcc pthread_create.c -pthread
#include <pthread.h>#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <errno.h>void Perror(const char *s){ perror(s); exit(EXIT_FAILURE);}void* fun2(void *arg){ pthread_t thread_id = pthread_self(); printf("the thread2 id is %ld, arg is %d\n", (long)thread_id, *(int*)arg); sleep(1); /* wait for join */ pthread_exit((void*)2);}void* fun3(void *arg){ pthread_t thread_id = pthread_self(); printf("the thread3 id is %ld, arg is %d\n", (long)thread_id, *(int*)arg); sleep(60); /* wait for cancel */}int main(){ int err; pthread_t thread1; pthread_t thread2; pthread_t thread3; int arg2 = 2; int arg3 = 3; thread1 = pthread_self(); printf("the thread1 id is %ld\n", (long)thread1); // Create thread err = pthread_create(&thread2, NULL, fun2, (void*)&arg2); if (err != 0) { Perror("can't create thread2\n"); } err = pthread_create(&thread3, NULL, fun3, (void*)&arg3); if (err != 0) { Perror("can't create thread3\n"); } // detach thread3 err = pthread_detach(thread3); if (err != 0) { Perror("can't detach thread3\n"); } // if equal if (pthread_equal(thread2, thread3) != 0) printf("the thread2 and thread3 same\n"); else printf("the thread2 and thread3 diff\n"); // wait thread2 exit void *retval; int res = pthread_join(thread2, &retval); if (res != 0) printf("can't join with thread2\n"); else printf("thread2 exit code %ld\n", (long)retval); // cancel thread3 err = pthread_cancel(thread3); if (err != 0) { Perror("can't cancel thread3\n"); } sleep(60); return 0;}
运行结果:
注:通过pstree命令也观察到线程数如程序设置一样。
参考:《unix环境高级编程》·第三版
End;
0 0
- linux多线程 编程详解
- linux多线程编程详解
- Linux多线程编程详解
- linux多线程编程详解
- Linux下多线程编程详解
- Linux下多线程编程详解
- Linux下多线程编程详解
- Linux下多线程编程详解
- Linux下多线程编程详解 .
- Linux下多线程编程详解
- 【Linux】Linux下多线程编程详解
- Linux多线程编程详解----条件变量pthread_cond_t
- Linux多线程编程详解----条件变量pthread_cond_t
- linux多线程网络编程详解(包含具体例子讲解)
- Java多线程编程详解
- Java多线程编程详解
- Java多线程编程详解
- Java多线程编程详解
- Jsoup爬数据+设置代理IP
- 英式英语 vs 美式英语
- POJ 2739
- [2016ACM多校] HDU5829 卷积 NTT
- Markdown编辑器对比分析
- linux多线程编程详解
- 以Hozz工具来管理hosts从而达到访问Google等部分合谐网站的目的
- Objective-c——理解与特性
- 深入类加载器二----主动引用和被动引用
- CodeForces 703D Mishka and Interesting sum (树状数组处理异或和)
- 趣好玩开发日志 02-day
- const常量
- 入门算法之冒泡排序
- POJ 1330 Nearest Common Ancestors(LCA,在线处理三种方式)