pthread_create的函数参数
时间: 2023-11-07 17:01:53 浏览: 55
pthread_create函数的参数包括四个:
1. 第一个参数是指向线程标识符的指针,它是一个pthread_t类型的变量。
2. 第二个参数是一个指向pthread_attr_t结构的指针,用于设置线程的属性。如果不需要设置特定的属性,可以将该参数设置为NULL。
3. 第三个参数是一个函数指针,用于指定新线程要执行的函数。该函数必须具有以下形式:`void* function_name(void* arg)`。函数的返回值是一个void指针。
4. 第四个参数是传递给新线程函数的参数。可以将任意类型的参数通过该参数传递给新线程函数。如果没有参数需要传递,可以将该参数设置为NULL。
例如,下面是一个使用pthread_create函数创建新线程的例子:
```c
#include <pthread.h>
#include <stdio.h>
void* thread_function(void* arg) {
int thread_arg = *(int*)arg;
printf("Hello from the new thread! Argument passed: %d\n", thread_arg);
pthread_exit(NULL);
}
int main() {
pthread_t thread_id;
int arg = 42;
pthread_create(&thread_id, NULL, thread_function, &arg);
pthread_join(thread_id, NULL);
return 0;
}
```