pthread 创建与销毁详解 pthread_create pthread_join pthread_exit pthread_detach

pthread 创建与销毁

pthread_create

  • 函数原型:

    int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                       void *(*start_routine)(void *), void *arg);
    
    • thread:指向 pthread_t 类型的指针,用于存储新线程的标识符。
    • attr:指向 pthread_attr_t 类型的指针,用于设置新线程的属性,为 NULL 则使用默认属性。
    • start_routine:指向函数的指针,新线程的运行入口。
    • arg:传递给 start_routine 的参数,可以为 NULL。
    • 返回值:成功返回 0,失败返回错误代码。
  • 函数作用:

    • 创建一个新的线程,并以 start_routine 指定的函数作为入口而开始执行。
  • 新线程停止方式:

    • 自身调用 pthread_exit,其结束状态码可以在主进程中通过 pthread_join 获得;
    • start_routine 返回,返回值等同于 pthread_exit 的结束状态码;
    • 在主线程中调用 pthread_cancel
    • 主进程中的任何线程调用 exit 函数或者主进程从 main 返回。
  • 注意事项:

    • 新线程的 cpu time clock0
    • 新线程将继承其父进程的 cpu 亲和性。

pthread_exit

  • 函数原型:

    void pthread_exit(void *retval);
    
    • retval:线程的退出状态,可以是任意类型的指针,可以为 NULL。
  • 函数作用:

    • 立即终止当前线程的执行,并将 retval 作为线程的退出状态返回给调用者。线程的退出状态可以通过其他线程使用 pthread_join 函数来获取。

pthread_join

  • 函数原型:

    int pthread_join(pthread_t thread, void **retval);
    
    • thread:要等待的线程标识符,其必须为 join-able。
    • retval:指向指针的指针,用于存储被等待线程的返回值,可以为 NULL。
    • 返回值:成功返回 0,失败返回错误代码。
  • 函数作用:

    • 阻塞调用它的线程,等待指定线程的结束,并获取其返回值。如果指定的线程早已结束,则立即返回。
  • 以下代码演示了 pthread_create,pthread_join,pthread_exit 的使用:

    #include <stdio.h>
    #include <unistd.h>
    #include <pthread.h>
    
    #define NEED_EXIT_STATUS (0)
    
    void *thread_func(void *arg)
    {
        printf("thread started\n");
        int *value = (int *)arg;
        // 输出为 10
        printf("received value: %d\n", *value);
        // 修改传入的值
        *value = 100;
        sleep(1);
    
    #if NEED_EXIT_STATUS
        // 需要返回结束状态码, 以下两种方法都可以,推荐使用 return
        // pthread_exit((void *)value);
        return value;
    #else
        // 不需要返回结束状态码,以下两种方法都可以,推荐使用 return
        // pthread_exit(NULL);
        return NULL;
    #endif
    }
    
    int main()
    {
        pthread_t tid;
        int value = 10;
        // 创建一个新线程,并以 thread_func 作为入口函数,传递给 thread_func 函数的参数为 value
        int ret = pthread_create(&tid, NULL, thread_func, (void *)&value);
        if (ret != 0) {
            fprintf(stderr, "pthread_create error: %d\n", ret);
            return -1;
        }
        printf("created new thread with id %ld\n", tid);
    
    #if NEED_EXIT_STATUS
        // 等待线程结束并获取退出状态
        void *thread_retval;
        ret = pthread_join(tid, &thread_retval);
        if (ret != 0) {
            fprintf(stderr, "pthread_join error: %d\n", ret);
            return -1;
        }
    
        int *retval = (int *)thread_retval;
        printf("thread exited with value: %d\n", *retval);
    #else
        // 等待线程结束不获取退出状态
        ret = pthread_join(tid, NULL);
        if (ret != 0) {
            fprintf(stderr, "pthread_join error: %d\n", ret);
            return -1;
        }
        printf("thread %ld exit normal\n", tid);
    #endif
    
        return 0;
    }
    

pthread_detach

  • 函数原型:

    int pthread_detach(pthread_t thread);
    
    • thread:要设置为分离状态的线程标识符。
    • 返回值:成功返回 0,失败返回错误代码。
  • 函数作用:

    • 将指定线程设置为“分离状态”,使得该线程结束时能够自动释放其资源。
  • 注意事项:

    • 一旦线程被设置为分离状态,就不能再调用 pthread_join 等待它的结束。
  • 以下代码演示了 pthread_detach 的使用:

    #include <stdio.h>
    #include <unistd.h>
    #include <pthread.h>
    
    #define NEED_EXIT_STATUS (0)
    
    void *thread_func(void *arg)
    {
        printf("thread started\n");
        sleep(1);
        printf("thread exited\n");
        return NULL;
    }
    
    int main() {
        pthread_t tid;
        int ret = pthread_create(&tid, NULL, thread_func, NULL);
        if (ret != 0) {
            fprintf(stderr, "pthread_create error: %d\n", ret);
            return -1;
        }
        printf("created new thread with id %ld\n", tid);
    
        ret = pthread_detach(tid);
        if (ret != 0) {
            fprintf(stderr, "pthread_detach error: %d\n", ret);
            return -1;
        }
        printf("thread detached, main thread exited\n");
        return 0;
    }
    
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

专注的罗哈哈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值