pthread_create 使用例子
时间: 2023-08-31 13:12:36 浏览: 77
好的,根据您的要求,以下是一个 pthread_create 的使用例子:
```
#include <pthread.h>
#include <stdio.h>
void *print_message_function(void *ptr) {
char *message;
message = (char *) ptr;
printf("%s \n", message);
}
int main() {
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int thread1_id, thread2_id;
thread1_id = pthread_create(&thread1, NULL, print_message_function, (void *) message1);
thread2_id = pthread_create(&thread2, NULL, print_message_function, (void *) message2);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}
```
使用 pthread_create 函数创建了两个线程,每个线程执行 print_message_function 函数并传递了不同的字符串参数。使用 pthread_join 函数等待线程执行完毕。
阅读全文