pthreads是一种POSIX线程库标准,可以在多个系统中使用。在C和C++中使用pthread库以及pthread_create函数来创建和控制线程。
一、pthread_create()介绍
pthread_create()函数用于创建一个线程。这个函数被大多数POSIX线程库实现支持。这个函数的原型如下:
#include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
pthread_create()的参数:
- thread: 指向pthread_t类型的指针,返回线程的真实标识符。
- attr(Optional): 线程属性。
- start_routine: 指向线程执行的函数。线程函数的类型为 void *(*start_routine) (void *arguments),其中arguments是传递给该线程函数的参数。
- arg: 传递给线程函数的参数。
二、创建线程的方法
1. 传递函数指针
我们可以像下面这样定义一个线程函数:
#include <stdio.h> #include <pthread.h> void* threadFunc1() { printf("Hello, this is threadFunc1\n"); pthread_exit(NULL); } int main() { pthread_t thread1; printf("Before creating thread\n"); pthread_create(&thread1, NULL, threadFunc1, NULL); printf("After creating thread\n"); pthread_exit(NULL); }
运行结果:
Before creating thread After creating thread Hello, this is threadFunc1
2. 传递一个对象提供线程函数访问
如下示例代码,我们可以定义一个结构,并将结构作为参数传递给线程函数。
#include <stdio.h> #include <pthread.h> struct thread_data { int thread_id; char *message; }; void* threadFunc2(void *args) { struct thread_data *data = (struct thread_data *) args; printf("Thread ID: %d\n", data -> thread_id); printf("Message: %s\n", data -> message); pthread_exit(NULL); } int main() { pthread_t thread2; struct thread_data data; data.thread_id = 123; data.message = "Hello, this is threadFunc2"; printf("Before creating thread\n"); pthread_create(&thread2, NULL, threadFunc2, (void *) &data); printf("After creating thread\n"); pthread_exit(NULL); }
运行结果:
Before creating thread After creating thread Thread ID: 123 Message: Hello, this is threadFunc2
三、线程属性
可以使用pthread_attr_init()函数来初始化一个线程属性对象,然后使用这个线程属性对象来设置线程的属性。以下示例代码展示如何使用pthread_attr_setdetachstate()设置线程的分离属性。
#include <stdio.h> #include <pthread.h> void* threadFunc3() { printf("Hello, this is threadFunc3\n"); pthread_exit(NULL); } int main() { pthread_t thread3; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); printf("Before creating thread\n"); pthread_create(&thread3, &attr, threadFunc3, NULL); printf("After creating thread\n"); pthread_attr_destroy(&attr); pthread_exit(NULL); }
运行结果:
Before creating thread After creating thread Hello, this is threadFunc3
四、线程ID和join()
每个线程都有一个唯一标识符的线程ID,可以使用pthread_self()函数获取当前线程的ID。
可以使用pthread_join()来等待线程终止。和wait()函数类似,pthread_join()会阻塞调用者直到指定的线程终止。以下示例代码在创建线程后等待线程终止。
#include <stdio.h> #include <pthread.h> void* threadFunc4() { printf("Hello, this is threadFunc4\n"); pthread_exit(NULL); } int main() { pthread_t thread4; printf("Before creating thread\n"); pthread_create(&thread4, NULL, threadFunc4, NULL); printf("After creating thread\n"); pthread_join(thread4, NULL); pthread_exit(NULL); }
运行结果:
Before creating thread After creating thread Hello, this is threadFunc4
五、总结
pthreads是一种POSIX线程库标准,可以在多个系统中使用。pthread_create()函数用于创建一个线程。可以传递函数指针或一个对象作为参数传递,也可以设置线程属性和获取线程ID。我们可以使用pthread_join()来等待线程终止。对于多线程编程,合理的控制线程并发和同步是很重要的。
原创文章,作者:KFXPO,如若转载,请注明出处:https://www.506064.com/n/362008.html