多线程同步是指在多线程编程中,控制多个线程对共享资源的访问,以避免数据不一致或竞争条件。Linux C 中常用的多线程同步机制包括互斥锁(Mutex)、条件变量(Condition Variable)、信号量(Semaphore)和读写锁(Read-Write Lock)等。
概念:互斥锁是一种同步机制,用于保护共享资源,确保同一时间只有一个线程可以访问该资源。
应用场景:适用于任何需要保护共享资源的场景,如全局变量、文件操作等。
示例代码:
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int shared_data = 0;
void* thread_func(void* arg) {
pthread_mutex_lock(&mutex);
shared_data++;
pthread_mutex_unlock(&mutex);
return NULL;
}
int main() {
pthread_t thread1, thread2;
pthread_create(&thread1, NULL, thread_func, NULL);
pthread_create(&thread2, NULL, thread_func, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("Shared data: %d\n", shared_data);
return 0;
}
概念:条件变量允许线程在某个条件成立时等待或通知其他线程。
应用场景:适用于生产者-消费者问题、线程池等需要线程间协作的场景。
示例代码:
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int ready = 0;
void* producer(void* arg) {
pthread_mutex_lock(&mutex);
ready = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
return NULL;
}
void* consumer(void* arg) {
pthread_mutex_lock(&mutex);
while (ready == 0) {
pthread_cond_wait(&cond, &mutex);
}
printf("Data is ready!\n");
pthread_mutex_unlock(&mutex);
return NULL;
}
int main() {
pthread_t producer_thread, consumer_thread;
pthread_create(&producer_thread, NULL, producer, NULL);
pthread_create(&consumer_thread, NULL, consumer, NULL);
pthread_join(producer_thread, NULL);
pthread_join(consumer_thread, NULL);
return 0;
}
概念:信号量是一种计数器,用于控制多个线程对共享资源的访问。
应用场景:适用于限制同时访问某一资源的线程数量,如连接池、资源池等。
示例代码:
#include <semaphore.h>
#include <pthread.h>
#include <stdio.h>
sem_t sem;
int shared_data = 0;
void* thread_func(void* arg) {
sem_wait(&sem);
shared_data++;
sem_post(&sem);
return NULL;
}
int main() {
sem_init(&sem, 0, 1); // 初始化信号量,初始值为1
pthread_t thread1, thread2;
pthread_create(&thread1, NULL, thread_func, NULL);
pthread_create(&thread2, NULL, thread_func, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("Shared data: %d\n", shared_data);
sem_destroy(&sem);
return 0;
}
概念:读写锁允许多个线程同时读取共享资源,但只允许一个线程写入共享资源。
应用场景:适用于读操作远多于写操作的场景,如缓存系统、配置文件读取等。
示例代码:
#include <pthread.h>
#include <stdio.h>
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
int shared_data = 0;
void* reader(void* arg) {
pthread_rwlock_rdlock(&rwlock);
printf("Read: %d\n", shared_data);
pthread_rwlock_unlock(&rwlock);
return NULL;
}
void* writer(void* arg) {
pthread_rwlock_wrlock(&rwlock);
shared_data++;
printf("Write: %d\n", shared_data);
pthread_rwlock_unlock(&rwlock);
return NULL;
}
int main() {
pthread_t reader_thread1, reader_thread2, writer_thread;
pthread_create(&reader_thread1, NULL, reader, NULL);
pthread_create(&reader_thread2, NULL, reader, NULL);
pthread_create(&writer_thread, NULL, writer, NULL);
pthread_join(reader_thread1, NULL);
pthread_join(reader_thread2, NULL);
pthread_join(writer_thread, NULL);
return 0;
}
原因:多个线程互相等待对方释放资源,导致程序无法继续执行。
解决方法:
原因:多个线程对共享资源的访问顺序不确定,导致结果不可预测。
解决方法:
__sync_fetch_and_add
)。原因:线程不断改变状态以响应其他线程,但没有任何进展。
解决方法:
通过合理选择和使用多线程同步机制,可以有效避免上述问题,确保程序的正确性和稳定性。
领取专属 10元无门槛券
手把手带您无忧上云