Linux 多线程函数解析

梧桐芭蕉 2012-05-16 02:24:58
加精
Linux多线程函数解析
Linux多线程函数用得比较多的是下面的3个
pthread_create(),pthread_exit(),pthread_join();它们都是在头文件之中。编译时需要加静态库-lpthread

下面是函数的说明:
  pthread_create是UNIX环境创建线程函数
int pthread_create(
pthread_t *restrict tidp,
const pthread_attr_t *restrict_attr,
void*(*start_rtn)(void*),
void *restrict arg);
返回值
  若成功则返回0,否则返回出错编号
  返回成功时,由tidp指向的内存单元被设置为新创建线程的线程ID。attr参数用于制定各种不同的线程属性。新创建的线程从start_rtn函数的地址开始运行,该函数只有一个万能指针参数arg,如果需要向start_rtn函数传递的参数不止一个,那么需要把这些参数放到一个结构中,然后把这个结构的地址作为arg的参数传入。
  linux下用C开发多线程程序,Linux系统下的多线程遵循POSIX线程接口,称为pthread。
  由 restrict 修饰的指针是最初唯一对指针所指向的对象进行存取的方法,仅当第二个指针基于第一个时,才能对对象进行存取。对对象的存取都限定于基于由 restrict 修饰的指针表达式中。 由 restrict 修饰的指针主要用于函数形参,或指向由 malloc() 分配的内存空间。restrict 数据类型不改变程序的语义。 编译器能通过作出 restrict 修饰的指针是存取对象的唯一方法的假设,更好地优化某些类型的例程。
参数
  第一个参数为指向线程标识符的指针。
  第二个参数用来设置线程属性。
  第三个参数是线程运行函数的起始地址。
  最后一个参数是运行函数的参数。
另外,在编译时注意加上-lpthread参数,以调用静态链接库。因为pthread并非Linux系统的默认库

pthread_exit(void* retval);
线程通过调用pthread_exit函数终止自身执行,就如同进程在结束时调用exit函数一样。这个函数的作用是,终止调用它的线程并返回一个指向某个对象的指针。该指针可以通过pthread_join(pthread_t tpid, void **value_ptr)中的第二个参数value_ptr获取到。

函数pthread_join用来等待一个线程的结束。函数原型为:
  extern int pthread_join __P (pthread_t __th, void **__thread_return);
第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指针,它可以用来存储被等待线程退出时的返回值。这个函数是一个线程阻塞的函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。如果执行成功,将返回0,如果失败则返回一个错误号。
所有线程都有一个线程号,也就是Thread ID。其类型为pthread_t。通过调用pthread_self()函数可以获得自身的线程号。

下面是一个简单的例子,子线程thread_fun会打出5次“this is thread_fun print!”然后调用pthread_exit退出,并返回一个指向字符串“this is thread return value!”的指针。在主函数里面调用pthread_join等待thread_fun线程结束,然后读取子线程的返回值到value中,再打印出来。
输出结果是:
pthread_create ok!
this is thread_fun print!
this is thread_fun print!
this is thread_fun print!
this is thread_fun print!
this is thread_fun print!
pthread exit value: this is thread return value!

01.#include
02.#include
03.#include
04.#include
05.#include
06.#include
07.//////////////////////////////////////////////////////
08.void *thread_fun(void *arg) {
09. int i=0;
10. char *value_ptr = "this is thread return value!\n";
11. for (i=0; i<5; i++) {
12. printf("this is thread_fun print!\n");
13. sleep(1);
14. }
15. pthread_exit((void*)value_ptr);
16.}
17.//////////////////////////////////////////////////////
18.int main(int argc, char **argv) {
19. pthread_t pid;
20. int ret;
21. void* value;
22.
23. ret = pthread_create(&pid, NULL, thread_fun, NULL);
24. if (ret) {
25. printf("pthread_create failed!\nerrno:%d\n", errno);
26. return -1;
27. }
28. printf("pthread_create ok!\n");
29.
30. pthread_join(pid, &value);
31. printf("pthread exit value: %s\n", value);
32. return 0;
33.}
34.
35.

...全文
4030 80 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
80 条回复
切换为时间正序
请发表友善的回复…
发表回复
vanxining 2012-05-26
  • 打赏
  • 举报
回复
[Quote=引用 37 楼 的回复:]
引用 28 楼 的回复:

引用 27 楼 的回复:

???
不理解~~~~~~~~
强大的linux.
windows有的机制linux一定有.
就算没有, 也能做成有.
什么时候我们有自己的系统. 去它的微软.
引用 25 楼 的回复:
linux没有线程,只有进程。请勿把win的概念混淆到linux下面。

windows的进程是个静态的概念,充其量就只是个……
[/Quote]
的确是误导人。Linux确实有线程的概念。参看《现代操作系统》等书籍。
milancottage 2012-05-24
  • 打赏
  • 举报
回复
深入浅出
LAONINGA098 2012-05-24
  • 打赏
  • 举报
回复
好东西~收藏了~~~~~
young8521480 2012-05-23
  • 打赏
  • 举报
回复
good
我不会Debug 2012-05-23
  • 打赏
  • 举报
回复
好文章 学习了 评论比较强大
CDBCDB1998 2012-05-22
  • 打赏
  • 举报
回复
现在多线程的情况,一般真正使用的地方比较少~
zhiweiarm 2012-05-22
  • 打赏
  • 举报
回复
分享一下
zenmell 2012-05-21
  • 打赏
  • 举报
回复
mimixi666

(什么都是新手)

等 级:

#71楼 得分:0回复于:2012-05-21 18:18:38。。。。。。。。。
XHB199371 2012-05-20
  • 打赏
  • 举报
回复
努力理解
nettoysfkf 2012-05-20
  • 打赏
  • 举报
回复
厉害!
csb5188 2012-05-20
  • 打赏
  • 举报
回复
前排占位,支持一下。

代码排一下版就好了。

正好问个问题,为什么在网上搜到的以及这帖子里面linux下c++代码的#include 后面都是空白
DavidDjs 2012-05-20
  • 打赏
  • 举报
回复
总结的很好 受用了
chenmingguo0809 2012-05-19
  • 打赏
  • 举报
回复
好东西,支持!
LAONINGA098 2012-05-19
  • 打赏
  • 举报
回复
不错。。。。支持!!!!
景语 2012-05-19
  • 打赏
  • 举报
回复
这个上首页推荐是不是有点失水准
garrett袁 2012-05-19
  • 打赏
  • 举报
回复
- -看下
dragon_cheng 2012-05-19
  • 打赏
  • 举报
回复
很好,但是……看不懂……
zhangdawei1976 2012-05-18
  • 打赏
  • 举报
回复
挺复杂,但是好东西
blackkettle 2012-05-18
  • 打赏
  • 举报
回复
Linux多线程,好东西!赞!
hello.wwwwww 2012-05-18
  • 打赏
  • 举报
回复
好东东啊
加载更多回复(31)

567

社区成员

发帖
与我相关
我的任务
社区描述
英特尔® 边缘计算,聚焦于边缘计算、AI、IoT等领域,为开发者提供丰富的开发资源、创新技术、解决方案与行业活动。
社区管理员
  • 英特尔技术社区
  • shere_lin
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧