11. 线程库 - <pthread.h>
POSIX线程库(pthread
)提供了一种在C语言中进行多线程编程的方法。通过多线程,可以使程序并行处理多项任务,从而提高性能和响应速度。
11.1. 线程创建与管理
多线程编程的重要组成部分是如何有效创建和管理线程。
11.1.1. 线程的创建:pthread_create
pthread_create
用于创建一个新线程,并使其开始运行。
- 使用函数:
pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
- 参数说明:
pthread_t *thread
: 指向创建的线程标识符的指针。const pthread_attr_t *attr
: 线程属性,可设为 NULL 以使用默认属性。void *(*start_routine)(void *)
: 线程运行的函数地址。void *arg
: 传递给线程函数的参数。
- 返回值:成功返回0,失败返回错误码。
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *thread_function(void *arg) {
printf("Hello from thread!\n");
return NULL;
}
int main() {
pthread_t thread;
if (pthread_create(&thread, NULL, thread_function, NULL)) {
fprintf(stderr, "Error creating thread\n");
return 1;
}
pthread_join(thread, NULL);
return 0;
}
11.1.2. 线程的终止与等待:pthread_exit
, pthread_join
-
pthread_exit
:线程可以通过调用这个函数自行终止,也可以不显式调用而结束。 -
pthread_join
:用于等待一个线程的结束。类似于进程中的wait
,它可以获取线程的退出状态。
#include <pthread.h>
#include <stdio.h>
void *thread_function(void *arg) {
printf("Thread is running\n");
pthread_exit(NULL); // 线程终止
}
int main() {
pthread_t thread;
pthread_create(&thread, NULL, thread_function, NULL);
pthread_join(thread, NULL); // 主线程等待线程结束
printf("Thread finished\n");
return 0;
}
11.2. 线程同步
在多线程环境中,确保线程安全至关重要。可以使用 mutex 和条件变量来实现线程同步。
11.2.1. 互斥锁:pthread_mutex_t
互斥锁(mutex)用于防止多个线程同时访问共享资源。
- 初始化:
pthread_mutex_init(&mutex, NULL);
- 加锁和解锁:
- 加锁:
pthread_mutex_lock(&mutex);
- 解锁:
pthread_mutex_unlock(&mutex);
- 加锁:
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t mutex;
void *thread_function(void *arg) {
pthread_mutex_lock(&mutex);
// 临界区,多个线程不能同时执行此部分
printf("Thread is running\n");
pthread_mutex_unlock(&mutex);
return NULL;
}
int main() {
pthread_mutex_init(&mutex, NULL);
pthread_t thread;
pthread_create(&thread, NULL, thread_function, NULL);
pthread_join(thread, NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
11.2.2. 条件变量:pthread_cond_t
条件变量用于线程间的复杂同步机制,使线程能够睡眠并等待某些条件满足。
- 初始化:
pthread_cond_init(&cond, NULL);
- 等待和唤醒:
- 等待:
pthread_cond_wait(&cond, &mutex);
- 唤醒:
pthread_cond_signal(&cond);
或pthread_cond_broadcast(&cond);
- 等待:
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
void *thread_function(void *arg) {
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex); // 等待条件变量
printf("Condition met, thread proceeding\n");
pthread_mutex_unlock(&mutex);
return NULL;
}
int main() {
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
pthread_t thread;
pthread_create(&thread, NULL, thread_function, NULL);
// 主线程睡眠一段时间,然后发信号唤醒
sleep(1);
pthread_mutex_lock(&mutex);
pthread_cond_signal(&cond); // 唤醒等待线程
pthread_mutex_unlock(&mutex);
pthread_join(thread, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
11.3. 读写锁与屏障
读写锁和屏障是高级线程同步工具。
11.3.1. 读写锁:pthread_rwlock_t
读写锁允许多线程同时进行“读”操作,而“写”操作是独占的。
- 初始化:
pthread_rwlock_init(&rwlock, NULL);
- 加锁和解锁:
- 读加锁:
pthread_rwlock_rdlock(&rwlock);
- 写加锁:
pthread_rwlock_wrlock(&rwlock);
- 解锁:
pthread_rwlock_unlock(&rwlock);
- 读加锁:
11.3.2. 屏障:pthread_barrier_t
屏障用于同步多个线程,使它们在某一点必须全部到达时才继续执行后续操作。
- 初始化:
pthread_barrier_init(&barrier, NULL, count);
- 等待:
pthread_barrier_wait(&barrier);
#include <pthread.h>
#include <stdio.h>
pthread_barrier_t barrier;
void *thread_function(void *arg) {
printf("Thread reached barrier\n");
pthread_barrier_wait(&barrier); // 等待其他线程到达此屏障
printf("Thread passed barrier\n");
return NULL;
}
int main() {
const int num_threads = 3;
pthread_barrier_init(&barrier, NULL, num_threads);
pthread_t threads[num_threads];
for (int i = 0; i < num_threads; i++)
pthread_create(&threads[i], NULL, thread_function, NULL);
for (int i = 0; i < num_threads; i++)
pthread_join(threads[i], NULL);
pthread_barrier_destroy(&barrier);
return 0;
}
通过使用上述线程库功能,您可以在多线程应用程序中实现高效及复杂的同步机制。