• Linux学习——线程的创建和回收


    目录

    一、线程的概念

     特点

    ​编辑

     注意

    二、线程的创建

    三、线程的结束

    四、线程间参数传递:(重点难点)

    五、线程的回收:

    使用pthread_join 函数:

    程序:

     结果:

     六、线程分离


    一、线程的概念

     特点

     

     注意

    Linux内核不提供线程,由线程库来实现。

    二、线程的创建

    #include 

     int  pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*routine)(void *), void *arg);

     成功返回0,失败时返回错误码

     thread 线程对象

     attr 线程属性,NULL代表默认属性

     routine 线程执行的函数

     arg 传递给routine的参数 ,参数是void * ,注意传递参数格式,

    注意事项:1. 主进程的退出,它创建的线程也会退出。

    线程创建需要时间,如果主进程马上退出,那线程不能得到执行

    三、线程的结束

    #include 

    void  pthread_exit(void *retval);

          

    结束当前线程

    retval可被其他线程通过pthread_join获取

    线程私有资源被释放

    获取线程的id

    通过pthread_create函数的第一个参数;通过在线程里面调用pthread_self函数

    四、线程间参数传递:(重点难点)

    createP_t.c:8:5: error: invalid use of void expression

         printf("input arg=%d\n",(int)*arg);

    1. 通过地址传递参数,注意类型的转换
    2. 值传递,这时候编译器会告警,需要程序员自己保证数据长度正确
    1. #if 1
    2. #include
    3. #include
    4. #include
    5. void *testThread(void *arg){
    6. printf("This is a thread test,pid=%d,tid=%lu\n",getpid(),pthread_self());
    7. // return NULL;
    8. printf("input arg=%d\n",*(int*)arg);//(int)arg
    9. pthread_exit(NULL);
    10. printf("after pthread exit\n");
    11. }
    12. int main(){
    13. pthread_t tid;
    14. int ret;
    15. int arg = 5;
    16. ret = pthread_create(&tid,NULL,testThread,(void *)&arg);//(void*)arg
    17. printf("This is main thread,tid=%lu\n",tid);
    18. sleep(1);
    19. }
    20. #endif
    21. #if 0
    22. #include
    23. #include
    24. #include
    25. int *testThread(char *arg){
    26. printf("This is a thread test pid = %d tid = %lu\n",getpid(),pthread_self());
    27. pthread_exit(NULL);
    28. printf("after pthread exit");
    29. }
    30. int main(){
    31. pthread_t tid;
    32. int ret;
    33. ret = pthread_create(&tid,NULL,(void*)testThread,NULL);
    34. printf("This is main Thread pid = %d tid = %lu\n",getpid(),tid);
    35. sleep(1);
    36. }
    37. #endif

    1. #if 1
    2. #include
    3. #include
    4. #include
    5. void *testThread(void *arg){
    6. printf("This is a thread test,pid=%d,tid=%lu\n",getpid(),pthread_self());
    7. // return NULL;
    8. printf("input arg=%d\n",*(int*)arg);//(int)arg
    9. pthread_exit(NULL);
    10. printf("after pthread exit\n");
    11. }
    12. int main(){
    13. pthread_t tid[5];
    14. int ret,i;
    15. int arg = 5;
    16. for(i = 0;i < 5;i++){
    17. ret = pthread_create(&tid[i],NULL,testThread,(void *)&i);//(void*)arg
    18. printf("This is main thread,tid=%lu\n",tid[i]);
    19. }
    20. sleep(1);
    21. }
    22. #endif

    五、线程的回收:

    使用pthread_join 函数:

    #include 

     int  pthread_join(pthread_t thread, void **retval);

    注意:pthread_join 是阻塞函数,如果回收的线程没有结束,则一直等待

    程序:

    1. #include
    2. #include
    3. #include
    4. void *func(void *arg){
    5. printf("This is child thread\n");
    6. //sleep(1);
    7. sleep(5);
    8. pthread_exit("thread return");
    9. }
    10. #if 0
    11. int main()
    12. {
    13. pthread_t tid;
    14. void *retv;
    15. pthread_create(&tid,NULL,func,NULL);
    16. pthread_join(tid,&retv);
    17. printf("thread ret=%s\n",(char*)retv);
    18. sleep(1);
    19. }
    20. #endif
    21. #if 1
    22. int main(){
    23. pthread_t tid[5];
    24. void *retv;
    25. int i;
    26. for(i=0;i<5;i++){
    27. pthread_create(&tid[i],NULL,func,NULL);
    28. }
    29. for(i=0;i<5;i++){
    30. pthread_join(tid[i],&retv);
    31. printf("thread ret=%s\n",(char*)retv);
    32. }
    33. //while(1){
    34. sleep(1);
    35. //}
    36. }
    37. #endif
    38. #if 0
    39. int main(){
    40. pthread_t tid[100];
    41. void *retv;
    42. int i;
    43. for(i=0;i<100;i++){
    44. pthread_create(&tid[i],NULL,func,NULL);
    45. }
    46. for(i=0;i<100;i++){
    47. // pthread_join(tid[i],&retv);
    48. // printf("thread ret=%s\n",(char*)retv);
    49. }
    50. while(1){
    51. sleep(1);
    52. }
    53. }
    54. #endif

     结果:

     

     六、线程分离

    程序:

    1. #include
    2. #include
    3. #include
    4. void *func(void *arg){
    5. pthread_detach(pthread_self());
    6. printf("This is child thread\n");
    7. sleep(5);
    8. pthread_exit("thread return");
    9. }
    10. int main(){
    11. pthread_t tid[5];
    12. void *retv;
    13. int i;
    14. for(i=0;i<5;i++){
    15. pthread_create(&tid[i],NULL,func,NULL);
    16. //pthread_detach(tid[i]);
    17. }
    18. while(1){
    19. sleep(1);
    20. }
    21. }

    结果:

    1. #include
    2. #include
    3. #include
    4. void *func(void *arg){
    5. printf("This is child thread\n");
    6. sleep(5);
    7. pthread_exit("thread return");
    8. }
    9. int main(){
    10. pthread_t tid[5];
    11. void *retv;
    12. int i;
    13. pthread_attr_t attr;
    14. pthread_attr_init(&attr);
    15. pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
    16. for(i=0;i<5;i++){
    17. pthread_create(&tid[i],&attr,func,NULL);
    18. // pthread_detach(tid);
    19. }
    20. while(1){
    21. sleep(1);
    22. }
    23. }

    使用线程的分离:

    两种方式:

    1 使用pthread_detach

    2 创建线程时候设置为分离属性

      pthread_attr_t attr;

      pthread_attr_init(&attr);

      pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

    验证实验:

    把创建线程增加到100个

    通过join、detach回收可以明显看出内存的减少,不用则会不减反增。

  • 相关阅读:
    广汽传祺E9上市,3DCAT实时云渲染助力线上3D高清看车体验
    程序员为讨好老婆,用go写了如下程序,细思极恐
    数据治理之主数据管理MDM
    【halcon】halcon轮廓总结之select_contours_xld
    QT点击事情实现图片切换
    读写锁三种关系的证明(读者和读者互补影响、写者和写者互斥、读者和写者互斥)
    李航老师《统计学习方法》阅读笔记
    QT事件系统_lineEdit输入信息触发事件方法
    二分查找示例2(寻找峰值)
    PyTorch实战:卷积神经网络详解+Python实现卷积神经网络Cifar10彩色图片分类
  • 原文地址:https://blog.csdn.net/qq_52479948/article/details/127739063