当前位置:   article > 正文

Linux线程——常用API_linux线程相关的api

linux线程相关的api

目录

线程创建

函数原型及头文件

参数解读

返回值

线程退出

函数原型及头文件

参数解读

线程等待

函数原型及头文件

参数解读

返回值

线程脱离

函数原型及头文件

参数解读

返回值

其他

线程ID获取

函数原型及头文件

返回值

线程比较

函数原型及头文件

参数解读

返回值

代码示例


线程创建

函数原型及头文件

  1. #include <pthread.h>
  2. int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);

参数解读

                tidp当pthread_create成功返回时,由tidp指向的内存单元被设置为新创建线程的线程ID。(指针,输入方式是地址)
                attr一般设为NULL。attr参数用于定制各种不同的线程属性,暂可以把它设置为NULL,以创建默认属性的线程。
(*start_rtn)(void *)新创建的线程的入口地址。新创建的线程从start_rtn函数的地址开始运行,该函数只有一个无类型指针参数arg。
                arg向start_rtn函数传递的参数。如果需要向start_rtn函数传递的参数不止一个,那么需要把这些参数放到一个结构中,然后把这个结构的地址作为arg参数传入。(指针,输入方式是地址,同时要注意是void *型)

返回值

若成功返回0,否则返回错误编号。

线程退出

单个线程可以通过以下三种方式退出,在不终止整个进程的情况下停止它的控制流:

1)线程只是从启动例程中返回,返回值是线程的退出码

2)线程可以被同一进程中的其他线程取消。

3)线程调用pthread_exit

函数原型及头文件

  1. #include <pthread.h>
  2. int pthread_exit(void *rval_ptr);

参数解读

rval_ptr:一个无类型指针,与传给启动例程的单个参数类似。进程中的其他线程可以通过调用pthread_join函数访问到这个指针。

线程等待

调用这个函数的线程将一直阻塞,直到指定的线程调用pthread_exit、从启动例程中返回或者被取消。如果例程只是从它的启动例程返回i,rval_ptr将包含返回码。如果线程被取消,由rval_ptr指定的内存单元就置为PTHREAD_CANCELED。

可以通过调用pthread_join自动把线程置于分离状态,这样资源就可以恢复。如果线程已经处于分离状态,pthread_join调用就会失败,返回EINVAL。

函数原型及头文件

  1. #include <pthread.h>
  2. int pthread_join(pthread_t thread, void **rval_ptr);

参数解读

thread线程名
**rval_ptr线程的返回值。如果对线程的返回值不感兴趣,可以把rval_ptr置为NULL。在这种情况下,调用pthread_join函数将等待指定的线程终止,但并不获得线程的终止状态。

返回值

若成功返回0,否则返回错误编号。

线程脱离

函数原型及头文件

  1. #include <pthread.h>
  2. int pthread_detach(pthread_t thread);

参数解读

一个线程或者是可汇合(joinable,默认值),或者是脱离的(detached)。当一个可汇合的线程终止时,它的线程ID和退出状态将留存到另一个线程对它调用pthread_join。脱离的线程却像守护进程,当它们终止时,所有相关的资源都被释放,我们不能等待它们终止。如果一个线程需要知道另一线程什么时候终止,那就最好保持第二个线程的可汇合状态。

pthread_detach函数把指定的线程转变为脱离状态。

返回值

若成功返回0,否则返回错误编号。

其他

本函数通常由想让自己脱离的线程使用,就如以下语句:

pthread_detach(pthread_self());

线程ID获取

函数原型及头文件

  1. #include <pthread.h>
  2. pthread_t pthread_self(void);

返回值

调用线程的ID。

线程比较

对于线程ID比较,为了可移植操作,我们不能简单地把线程ID当作整数来处理,因为不同系统对线程ID的定义可能不一样。

函数原型及头文件

  1. #include <pthread.h>
  2. int pthread_equal(pthread_t tid1, pthread_t tid2);

参数解读

tid1线程1的名字
tid2线程1的名字

返回值

若相等则返回非0值,否则返回0。

代码示例

线程返回数字打印

  1. #include <stdio.h>
  2. #include <pthread.h>
  3. void *func1(void *arg)
  4. {
  5. static int ret = 1;//static保留数据 不然函数调用后空间无效
  6. printf("t1:this thread t1:%ld \n",(unsigned long)pthread_self());//self是void型 需对其进行强转
  7. printf("t1:num=%d\n",*((int *)arg));//传参过来的是主函数的num num取地址传过来需先将void型转成int型 再通过指针指向其地址得到数据
  8. pthread_exit((void *)&ret);//线程的退出 可附带线程的返回值 可通过pthread_join调用得到该数据
  9. }
  10. int main()
  11. {
  12. int ret;
  13. int num =999;
  14. int *pret;
  15. pthread_t t1;
  16. ret = pthread_create(&t1,NULL,func1,(void *)&num);//第四个参数是void *型 而num是int型 需要对其进行强转 同时其输出为指针 则需要加上地址符号
  17. if(ret == 0)//判断线程是否创建成功
  18. {
  19. printf("main:create thread success\n");
  20. }
  21. printf("main:%ld\n",(unsigned long)pthread_self());
  22. pthread_join(t1,(void **)&pret);//线程等待 等待上面函数退出之后再执行下面语句
  23. printf("main:ti quit %d\n",*pret);//打印出线程的返回值
  24. return 0;
  25. }

线程返回字符串打印

  1. #include <stdio.h>
  2. #include <pthread.h>
  3. void *func1(void *arg)
  4. {
  5. static char *p = "hello word";
  6. printf("t1:this thread t1:%ld \n",(unsigned long)pthread_self());
  7. printf("t1:num=%d\n",*((int *)arg));
  8. pthread_exit((void *)p);
  9. }
  10. int main()
  11. {
  12. int ret;
  13. int num =999;
  14. char *pret;
  15. pthread_t t1;
  16. ret = pthread_create(&t1,NULL,func1,(void *)&num);
  17. if(ret == 0)
  18. {
  19. printf("main:create thread success\n");
  20. }
  21. printf("main:%ld\n",(unsigned long)pthread_self());
  22. pthread_join(t1,(void **)&pret);
  23. printf("main:ti quit %s\n",pret);//字符串的名字为地址 不需要加地址符
  24. return 0;
  25. }

线程共享内存空间

  1. #include <stdio.h>
  2. #include <pthread.h>
  3. int data = 0;
  4. void *func1(void *arg)
  5. {
  6. printf("t1:this thread t1:%ld \n",(unsigned long)pthread_self());
  7. printf("t1:num=%d\n",*((int *)arg));
  8. while(1)
  9. {
  10. printf("t1:%d\n",data++);
  11. sleep(1);
  12. }
  13. }
  14. void *func2(void *arg)
  15. {
  16. printf("t2:this thread t2:%ld \n",(unsigned long)pthread_self());
  17. printf("t2:num=%d\n",*((int *)arg));
  18. while(1)
  19. {
  20. printf("t2:%d\n",data++);
  21. sleep(1);
  22. }
  23. }
  24. int main()
  25. {
  26. int ret;
  27. int num =999;
  28. pthread_t t1;
  29. pthread_t t2;
  30. ret = pthread_create(&t1,NULL,func1,(void *)&num);
  31. if(ret == 0)
  32. {
  33. printf("main:create t1 success\n");
  34. }
  35. ret = pthread_create(&t2,NULL,func2,(void *)&num);
  36. if(ret == 0)
  37. {
  38. printf("main:create t2 success\n");
  39. }
  40. printf("main:%ld\n",(unsigned long)pthread_self());
  41. while(1)
  42. {
  43. printf("main:%d\n",data++);
  44. sleep(1);
  45. }
  46. pthread_join(t1,NULL);
  47. pthread_join(t2,NULL);
  48. return 0;
  49. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/936407?site
推荐阅读
相关标签
  

闽ICP备14008679号