掌握C11并发编程:解锁C++多线程高效秘籍

引言
随着计算机硬件性能的提升,多线程编程已经成为现代软件开发中不可或缺的一部分。C11标准对C语言进行了扩展,引入了对并发编程的支持。本文将深入探讨C11并发编程的相关知识,帮助读者解锁C++多线程高效秘籍。
C11并发编程基础
1. 线程
在C11中,线程可以通过 头文件提供的API来创建和管理。以下是一个创建线程的示例:
#include #include

int thread_function(void *arg) {
printf(“Thread ID: %ld\n”, thrd_id());
return 0;
}

int main() {
thrd_t thread;
if (thrd_create(&thread, thread_function, NULL) != thrd_success) {
perror(“thrd_create”);
return 1;
}
thrd_join(thread, NULL);
return 0;
}
2. 锁
为了保证线程间的数据一致性,需要使用锁来同步线程访问共享资源。C11提供了互斥锁(mutex)和读写锁(rwlock)。
#include #include

thrd_mutex_t lock;

int thread_function(void *arg) {
thrd_mutex_lock(&lock);
printf(“Thread ID: %ld, Lock acquired\n”, thrd_id());
thrd_mutex_unlock(&lock);
return 0;
}

int main() {
thrd_t thread1, thread2;
thrd_mutex_init(&lock, THRD_MUTEX的正常);
if (thrd_create(&thread1, thread_function, NULL) != thrd_success ||
thrd_create(&thread2, thread_function, NULL) != thrd_success) {
perror(“thrd_create”);
return 1;
}
thrd_join(thread1, NULL);
thrd_join(thread2, NULL);
thrd_mutex_destroy(&lock);
return 0;
}
3. 条件变量
条件变量用于在线程间进行通信,使得线程可以在满足特定条件之前阻塞。以下是一个使用条件变量的示例:
#include #include
#include

thrd_mutex_t lock;
thrd_cond_t cond;

int thread_function(void *arg) {
int *num = (int *)arg;
thrd_mutex_lock(&lock);
*num = 42;
thrd_cond_signal(&cond);
thrd_mutex_unlock(&lock);
return 0;
}

int main() {
thrd_t thread;
int num = 0;
thrd_mutex_init(&lock, THRD_MUTEX的正常);
thrd_cond_init(&cond);
if (thrd_create(&thread, thread_function, &num) != thrd_success) {
perror(“thrd_create”);
return 1;
}
thrd_mutex_lock(&lock);
thrd_cond_wait(&cond, &lock);
thrd_mutex_unlock(&lock);
printf(“Number: %d\n”, num);
thrd_join(thread, NULL);
thrd_mutex_destroy(&lock);
thrd_cond_destroy(&cond);
return 0;
}
高效并发编程技巧
1. 线程池
使用线程池可以减少线程创建和销毁的开销,提高程序的效率。以下是一个简单的线程池实现:
#include #include
#include
#include

#define MAX_THREADS 10

typedef struct {
void (*function)(void *);
void *arg;
} task;

pthread_mutex_t lock;
pthread_cond_t cond;
pthread_t threads[MAX_THREADS];
int active_threads = 0;
int task_count = 0;

void *thread_function(void *arg) {
while (1) {
pthread_mutex_lock(&lock);
while (task_count == 0 && active_threads < MAX_THREADS) { active_threads++; pthread_cond_wait(&cond, &lock); } if (task_count > 0) {
task task = tasks[0];
tasks = &tasks[1];
task_count–;
pthread_mutex_unlock(&lock);
task.function(task.arg);
pthread_mutex_lock(&lock);
} else {
pthread_mutex_unlock(&lock);
break;
}
}
return NULL;
}

int main() {
pthread_mutex_init(&lock, NULL);
pthread_cond_init(&cond, NULL);
for (int i = 0; i < MAX_THREADS; i++) { if (pthread_create(&threads[i], NULL, thread_function, NULL) != 0) { perror("pthread_create"); return 1; } } // 添加任务 task tasks[MAX_THREADS]; for (int i = 0; i < MAX_THREADS; i++) { tasks[i].function = &do_task; tasks[i].arg = (void *)&i; task_count++; } // 等待所有任务完成 for (int i = 0; i < MAX_THREADS; i++) { pthread_cond_signal(&cond); } // 等待线程池完成 for (int i = 0; i < MAX_THREADS; i++) { pthread_join(threads[i], NULL); } pthread_mutex_destroy(&lock); pthread_cond_destroy(&cond); return 0; } void do_task(void *arg) { int i = *(int *)arg; printf("Thread %d is doing task %d\n", i, i); sleep(1); } 2. 线程安全的数据结构 在多线程环境中,使用线程安全的数据结构可以避免数据竞争和死锁等问题。以下是一些常见的线程安全数据结构: 互斥锁(Mutex) :用于保护共享数据,确保同一时间只有一个线程可以访问。 读写锁(Rwlock) :允许多个线程同时读取数据,但只允许一个线程写入数据。 条件变量(Cond) :允许线程在满足特定条件之前阻塞,并在条件满足时唤醒其他线程。 原子操作(Atomic) :用于确保对共享数据的操作是原子的,防止数据竞争。 总结 C11并发编程为开发者提供了丰富的工具和API,可以帮助我们更高效地利用多核处理器。通过掌握线程、锁、条件变量等并发编程技术,以及使用线程池和线程安全的数据结构,我们可以解锁C++多线程高效秘籍,开发出性能优异的应用程序。