线程创建函数 pthread_create()

pthread_create是用于创建新线程的函数,它接受线程标识符、属性、启动函数和参数。线程函数可以自定义,返回值类型为void*,参数类型为void*,允许通过结构体传递多个参数。线程创建成功后,新线程会执行指定的函数直到返回,此时线程结束。
摘要由CSDN通过智能技术生成

pthread_create函数用于创建一个新线程并使其运行指定的函数。以下是每个参数的详细解释:

  1. pthread_t *thread:此参数是一个指向pthread_t类型的指针,用于接收新创建的线程的标识符。pthread_t类型是一个无符号长整型(unsigned long int)。
  2. const pthread_attr_t *attr:此参数是一个指向pthread_attr_t类型的指针,用于设置新线程的属性。如果传入NULL,那么将使用默认的线程属性。要自定义线程属性,请使用pthread_attr_initpthread_attr_setdetachstate等函数。
  3. void *(*start_routine) (void *):此参数是一个指向函数的指针。该函数将在新线程中运行,函数的返回值类型为void *,并接受一个void *类型的参数。
  4. void *arg:此参数是一个指向void的指针,将被传递给start_routine函数。

pthread_create函数的原码通常位于glibc库中,以下是一个简化版的实现,仅供参考:

#include <pthread.h>

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                   void *(*start_routine) (void *), void *arg) {
    // 创建线程并设置线程属性
    int ret = clone(start_routine, stack, flags, arg);

    if (ret == -1) {
        // 线程创建失败
        return -1;
    } else {
        // 线程创建成功,将线程ID存储在thread指向的内存中
        *thread = ret;
        return 0;
    }
}

关于第三个形参的格式要求,start_routine是一个函数指针,函数的原型应为:

void *function_name(void *arg);

其中,function_name是你自定义的函数名,arg是传递给函数的参数。函数应返回一个void *类型的指针。关于pthread_create及其参数的详细信息,可以在pthreads(7)手册页或相关在线文档中查阅。

void *(start_routine)(void) 是一个函数指针,它指向一个函数,该函数的参数为一个 void 指针类型,返回值也是一个 void 指针类型。

通常情况下,这个参数用于线程创建函数 pthread_create() 中,用于指定线程要运行的函数。具体来说,它定义了线程的入口点。

例如,假设我们有一个函数 foo,它的定义如下:

void* foo(void* arg) {
    // 这里是线程要执行的代码
    return NULL;
}

我们可以使用 pthread_create() 函数来创建一个新的线程,并让它执行 foo 函数:

pthread_t thread;
void* arg = NULL;
int ret = pthread_create(&thread, NULL, foo, arg);
if (ret != 0) {
    // 线程创建失败
}

在上面的代码中,第三个参数 foo 就是一个 void* ()(void) 类型的函数指针,它指向了我们要在新线程中执行的函数。在线程创建成功后,新线程将会执行 foo 函数中的代码,并在函数返回后自动结束线程。

需要注意的是,线程函数的参数必须是一个 void* 类型的指针,因为 pthread_create() 函数要求这样的参数类型。如果线程函数不需要参数,可以将参数指定为 NULL。在线程函数中,可以通过将 void* 类型的参数转换为需要的类型来获取参数的值。

线程函数并不一定只能有一个形参。线程函数的形参个数和类型取决于具体的实现,通常可以根据需要自由定义。

在 POSIX 线程(pthread)中,线程函数的原型是:

void *start_routine(void *arg);

其中,arg 参数是线程函数的形参,可以是任何类型的指针类型。在使用 pthread_create() 函数创建线程时,可以将一个指向线程函数的指针作为参数传递给该函数,该指针指向的函数可以包含多个形参。

例如,下面的线程函数有两个形参:

void *thread_func(void *arg1, void *arg2) {
    // 线程函数的实现
    return NULL;
}

在使用 pthread_create() 函数创建线程时,可以将指向 thread_func() 函数的指针作为参数传递给该函数,同时将需要的参数作为一个结构体打包传递给线程函数:

typedef struct {
    int arg1;
    char *arg2;
} ThreadArgs;

void *thread_func(void *args) {
    ThreadArgs *thread_args = (ThreadArgs *)args;
    int arg1 = thread_args->arg1;
    char *arg2 = thread_args->arg2;
    // 线程函数的实现
    return NULL;
}

int main() {
    ThreadArgs thread_args = {42, "hello"};
    pthread_t thread;
    pthread_create(&thread, NULL, thread_func, (void *)&thread_args);
    // ...
}

在上述代码中,我们将两个参数打包成一个结构体 ThreadArgs,并将其作为 void* 类型的参数传递给线程函数。在线程函数中,我们可以通过将 void* 类型的参数转换为 ThreadArgs* 类型来获取参数的值。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值