• linux线程创建等待及退出总结


    线程操作

    线程操作分线程的创建,退出,等待 3 种

    1. 线程创建

    #include 
    int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
    // 返回:若成功返回0,否则返回错误编号

      当pthread_create成功返回时,由tidp指向的内存单元被设置为新创建线程的线程ID。attr参数用于定制各种不同的线程属性,暂可以把它设置为NULL,以创建默认属性的线程。

      新创建的线程从start_rtn函数的地址开始运行,该函数只有一个无类型指针参数arg。如果需要向start_rtn函数传递的参数不止一个,那么需要把这些参数放到一个结构中,然后把这个结构的地址作为arg参数传入。

    2. 线程退出

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

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

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

      3)线程调用pthread_exit:

    #include 
    int pthread_exit(void *rval_ptr);

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

    3. 线程等待

    #include 
    int pthread_join(pthread_t thread, void **rval_ptr);
    // 返回:若成功返回0,否则返回错误编号

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

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

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

    4. 线程脱离

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

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

    #include 
    int pthread_detach(pthread_t thread);
    // 返回:若成功返回0,否则返回错误编号

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

    pthread_detach(pthread_self());

    5. 线程ID获取及比较

    #include 
    pthread_t pthread_self(void);
    // 返回:调用线程的ID

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

    #include 
    int pthread_equal(pthread_t tid1, pthread_t tid2);
    // 返回:若相等则返回非0值,否则返回0

    三段代码示例:

    (1)

    1. #include
    2. #include
    3. //int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
    4. void *func1(void *arg)
    5. {
    6.     printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
    7.     printf("t1:param is %d\n",*((int *)arg));
    8. }
    9. int main()
    10. {
    11.     int ret;
    12.     int param = 100;
    13.     pthread_t t1;
    14.     ret = pthread_create(&t1, NULL, func1,(void *)¶m);
    15.     if(ret == 0){
    16.         printf("main:create t1 success\n");
    17.     }
    18.     printf("main:%ld\n",(unsigned long)pthread_self());
    19.     pthread_join(t1,NULL);
    20.     return 0;
    21. }


    (2)

    1. #include
    2. #include
    3. //int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
    4. void *func1(void *arg)
    5. {
    6.     static int ret = 10;
    7.     printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
    8.     printf("t1:param is %d\n",*((int *)arg));
    9.     pthread_exit((void *)&ret);
    10. }
    11. int main()
    12. {
    13.     int ret;
    14.     int param = 100;
    15.     pthread_t t1;
    16.     int *pret = NULL;
    17.     ret = pthread_create(&t1, NULL, func1,(void *)¶m);
    18.     if(ret == 0){
    19.         printf("main:create t1 success\n");
    20.     }
    21.     printf("main:%ld\n",(unsigned long)pthread_self());
    22.     pthread_join(t1,(void **)&pret);
    23.     printf("main: t1 quit: %d\n",*pret);
    24.     return 0;
    25. }


    (3)

    1. #include
    2. #include
    3. //int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
    4. void *func1(void *arg)
    5. {
    6.     static char *p = "t1 is run out";
    7.     printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
    8.     printf("t1:param is %d\n",*((int *)arg));
    9.     pthread_exit((void *)p);
    10. }
    11. int main()
    12. {
    13.     int ret;
    14.     int param = 100;
    15.     pthread_t t1;
    16.     char *pret = NULL;
    17.     ret = pthread_create(&t1, NULL, func1,(void *)¶m);
    18.     if(ret == 0){
    19.         printf("main:create t1 success\n");
    20.     }
    21.     printf("main:%ld\n",(unsigned long)pthread_self());
    22.     pthread_join(t1,(void **)&pret);
    23.     printf("main: t1 quit: %s\n",pret);
    24.     return 0;
    25. }


    编译结果:

    线程还能共享内存空间

    示例代码:

    1. #include
    2. #include
    3. //int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
    4. int g_data = 0;
    5. void *func1(void *arg)
    6. {
    7.     printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
    8.     printf("t1:param is %d\n",*((int *)arg));
    9.     while(1){
    10.         printf("t1: %d\n",g_data++);    
    11.         sleep(1);
    12.         if(g_data == 3){
    13.             pthread_exit(NULL);
    14.         }
    15.     }
    16. }
    17. void *func2(void *arg)
    18. {
    19.     printf("t2:%ld thread is create\n",(unsigned long)pthread_self());
    20.     printf("t2:param is %d\n",*((int *)arg));
    21.     while(1){
    22.         printf("t2: %d\n",g_data++);    
    23.         sleep(1);
    24.     }
    25. }
    26. int main()
    27. {
    28.     int ret;
    29.     int param = 100;
    30.     pthread_t t1;
    31.     pthread_t t2;
    32.     ret = pthread_create(&t1, NULL, func1,(void *)¶m);
    33.     if(ret == 0){
    34.         printf("main:create t1 success\n");
    35.     }
    36.     ret = pthread_create(&t2, NULL, func2,(void *)¶m);
    37.     if(ret == 0){
    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",g_data++);    
    44.         sleep(1);
    45.     }
    46.     pthread_join(t1,NULL);
    47.     pthread_join(t2,NULL);
    48.     return 0;
    49. }


    编译结果:

  • 相关阅读:
    文心一言 VS 讯飞星火 VS chatgpt (71)-- 算法导论7.1 1题
    10.cuBLAS开发指南中文版--cuBLAS中的logger配置
    数据分表Mybatis Plus动态表名最优方案的探索
    windows10下SQL Prompt安装和注册
    Java并发—利用AQS实现自定义锁
    Linux-MySQL数据库之主从复制与读写分离
    多目标跟踪算法方案总结
    VMware Explore 2022 China,赋能中国企业加速实现云智能
    JCP系列电磁铁电源各种型号的技术参数
    比较好用的服务器和网站在线测速工具分享
  • 原文地址:https://blog.csdn.net/m0_74712453/article/details/130856099