pthread_create_detached详解

发布时间:2023-05-19

pthread_create_detached() 是一个与 POSIX 线程相关的函数,用于创建一个已分离的线程。在本文中,我们将详细探讨它的用法及相关内容。

一、简介

pthread_create_detached() 是线程库中的一个函数,用于创建一个已分离的线程。创建线程可用于多任务处理,同时不会对主程序造成阻塞。如果线程创建为已分离状态,则主程序可以继续执行,而线程在完成后会自行释放资源。 创建一个已分离的线程,可以通过如下方式:

#include <pthread.h>
int pthread_create_detached(pthread_t * thread, pthread_attr_t * attr, void *(*start_routine)(void *), void * arg);

其中,pthread_t * thread 表示创建的线程 ID,在函数调用后会返回一个线程 ID,方便后续的线程控制;pthread_attr_t * attr 表示线程的属性,如线程堆栈大小、调度策略等;void *(*start_routine)(void *) 表示线程开始时的回调函数;void * arg 表示线程开始时的参数。 下面,我们将从不同的角度来讲解这些参数。

二、线程 ID

pthread_create_detached() 函数通过输出参数返回创建的线程 ID。这个线程 ID 可以用于后续的线程控制,如线程取消、线程等待等。线程 ID 是一个整数,在不同的线程函数中都可以使用,用于标示当前线程。下面是一个简单的示例:

#include <stdio.h>
#include <pthread.h>
void* thread_func(void *arg) {
    printf("thread running\n");
    pthread_exit(NULL);
}
int main() {
    pthread_t tid;
    printf("main thread\n");
    pthread_create_detached(&tid, NULL, &thread_func, NULL);
    pthread_join(tid, NULL);
    printf("main done\n");
    return 0;
}

在上面的示例中,我们首先在主线程中调用了 pthread_create_detached() 函数来创建一个已分离的线程,并输出线程 ID。随后,我们使用 pthread_join() 函数来等待线程完成。

三、线程属性

在创建线程时,可以通过 pthread_attr_t 结构体来指定线程的属性。这些属性包括线程的堆栈大小、线程调度策略等。下面是一个示例:

#include <stdio.h>
#include <pthread.h>
void* thread_func(void *arg) {
    printf("thread running\n");
    pthread_exit(NULL);
}
int main() {
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    pthread_t tid;
    printf("main thread\n");
    pthread_create(&tid, &attr, &thread_func, NULL);
    pthread_attr_destroy(&attr);
    printf("main done\n");
    return 0;
}

在上面的示例中,我们使用了 pthread_attr_init()pthread_attr_destroy() 函数来初始化和销毁线程属性对象。其中,pthread_attr_setdetachstate() 函数用于设置线程的分离状态为已分离,这意味着线程完成后会自动释放资源。

四、线程回调函数

pthread_create_detached() 函数的第三个参数是线程开始时的回调函数。这个函数带有一个 void 指针类型参数,可以传递任意类型的参数。下面是一个示例:

#include <stdio.h>
#include <pthread.h>
void* thread_func(void *arg) {
    int i = *((int*)arg);
    printf("thread running: %d\n", i);
    pthread_exit(NULL);
}
int main() {
    int arg = 123;
    pthread_t tid;
    printf("main thread\n");
    pthread_create_detached(&tid, NULL, &thread_func, &arg);
    printf("main done\n");
    return 0;
}

在上面的示例中,我们将一个整数值作为线程回调函数的参数传递给了线程。在线程函数中,我们将这个值转换成整型并输出。这个功能可以扩展为将任何类型的数据传递给线程函数。

五、线程的终止

在使用线程时,一般都有一个线程退出的过程。线程可以自行退出,也可以由其父线程终止。下面是一些重要函数的示例:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define THREAD_NUM 5
void* thread_func(void *arg) {
    int i = *((int*)arg);
    printf("thread running: %d\n", i);
    pthread_exit(NULL);
}
int main() {
    pthread_t tid[THREAD_NUM];
    for (int i = 0; i < THREAD_NUM; i++) {
        int* arg = (int*)malloc(sizeof(int));
        *arg = i;
        pthread_create_detached(&tid[i], NULL, &thread_func, arg);
    }
    // 等待所有线程完成
    for (int i = 0; i < THREAD_NUM; i++) {
        pthread_join(tid[i], NULL);
    }
    printf("main done\n");
    return 0;
}

在上面的示例中,我们使用了 pthread_exit() 函数来退出线程。这个函数可以带有一个 void 指针类型的返回值,并将其返回给父线程。在示例中,我们使用了 pthread_join() 函数来等待线程完成。

六、小结

本文详细介绍了 pthread_create_detached() 函数的用法及相关内容,并从不同的角度探讨了线程 ID、线程属性、线程回调函数和线程的终止等方面。在使用线程时,需要注意线程之间的同步和互斥,以及线程安全等问题,这些内容将在后续文章中进行讲解。