在多线程编程中,线程共享进程资源是一个关键问题。线程共享进程资源意味着多个线程可以访问和修改相同的内存空间、文件句柄等资源。以下是几种常见的线程数据共享方法,以及它们的详细解释。
1. 全局变量
最简单的线程数据共享方法是使用全局变量。全局变量在进程的整个生命周期内都是可访问的,因此所有线程都可以访问和修改它。
代码示例
#include <stdio.h>
#include <pthread.h>
int global_var = 0;
void* thread_func(void* arg) {
global_var += 1;
printf("Thread %d: global_var = %d\n", *(int*)arg, global_var);
return NULL;
}
int main() {
pthread_t threads[10];
int thread_ids[10];
for (int i = 0; i < 10; i++) {
thread_ids[i] = i;
pthread_create(&threads[i], NULL, thread_func, &thread_ids[i]);
}
for (int i = 0; i < 10; i++) {
pthread_join(threads[i], NULL);
}
printf("Final value of global_var: %d\n", global_var);
return 0;
}
2. 线程局部存储(Thread Local Storage,TLS)
TLS 允许每个线程都有自己的变量副本,这些变量在所有线程之间是隔离的。当线程需要访问特定的数据时,可以使用 TLS。
代码示例
#include <stdio.h>
#include <pthread.h>
__thread int thread_local_var = 0;
void* thread_func(void* arg) {
thread_local_var += 1;
printf("Thread %d: thread_local_var = %d\n", *(int*)arg, thread_local_var);
return NULL;
}
int main() {
pthread_t threads[10];
int thread_ids[10];
for (int i = 0; i < 10; i++) {
thread_ids[i] = i;
pthread_create(&threads[i], NULL, thread_func, &thread_ids[i]);
}
for (int i = 0; i < 10; i++) {
pthread_join(threads[i], NULL);
}
return 0;
}
3. 线程条件变量(Thread Condition Variables)
线程条件变量允许线程在满足特定条件之前等待,并在条件满足时被唤醒。条件变量通常与互斥锁一起使用。
代码示例
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t lock;
pthread_cond_t cond;
void* producer(void* arg) {
for (int i = 0; i < 10; i++) {
pthread_mutex_lock(&lock);
// 生产数据
printf("Producer: produced data %d\n", i);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&lock);
}
return NULL;
}
void* consumer(void* arg) {
for (int i = 0; i < 10; i++) {
pthread_mutex_lock(&lock);
pthread_cond_wait(&cond, &lock);
// 消费数据
printf("Consumer: consumed data %d\n", i);
pthread_mutex_unlock(&lock);
}
return NULL;
}
int main() {
pthread_t producer_thread, consumer_thread;
pthread_mutex_init(&lock, NULL);
pthread_cond_init(&cond, NULL);
pthread_create(&producer_thread, NULL, producer, NULL);
pthread_create(&consumer_thread, NULL, consumer, NULL);
pthread_join(producer_thread, NULL);
pthread_join(consumer_thread, NULL);
pthread_mutex_destroy(&lock);
pthread_cond_destroy(&cond);
return 0;
}
4. 线程间通信(Inter-Thread Communication,ITC)
线程间通信允许线程之间交换数据。常见的 ITC 方法包括信号量、消息队列、共享内存等。
代码示例
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t lock;
pthread_cond_t cond;
int shared_data = 0;
void* producer(void* arg) {
for (int i = 0; i < 10; i++) {
pthread_mutex_lock(&lock);
shared_data = i;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&lock);
}
return NULL;
}
void* consumer(void* arg) {
for (int i = 0; i < 10; i++) {
pthread_mutex_lock(&lock);
pthread_cond_wait(&cond, &lock);
printf("Consumer: received data %d\n", shared_data);
pthread_mutex_unlock(&lock);
}
return NULL;
}
int main() {
pthread_t producer_thread, consumer_thread;
pthread_mutex_init(&lock, NULL);
pthread_cond_init(&cond, NULL);
pthread_create(&producer_thread, NULL, producer, NULL);
pthread_create(&consumer_thread, NULL, consumer, NULL);
pthread_join(producer_thread, NULL);
pthread_join(consumer_thread, NULL);
pthread_mutex_destroy(&lock);
pthread_cond_destroy(&cond);
return 0;
}
总结
以上介绍了几种常见的线程数据共享方法。在实际应用中,选择合适的方法取决于具体需求和场景。合理地使用线程数据共享可以提高程序的性能和可维护性。
