时间:2023-05-29 来源:网络 人气:
在多线程编程中,同步机制是不可或缺的一部分。本文将介绍Linux下常用的同步方法,帮助读者更好地理解和应用同步机制,提高程序的效率和稳定性。
一、互斥锁(Mutex)
互斥锁是最基本的同步机制之一。它可以保证在任意时刻只有一个线程可以访问共享资源。当一个线程获得了互斥锁后,其他试图获取该锁的线程将被阻塞,直到持有该锁的线程释放锁为止。
下面是一个简单的使用互斥锁的例子:
c
#include<stdio.h>
#include<pthread.h>
pthread_mutex_tmutex=PTHREAD_MUTEX_INITIALIZER;
intcount=0;
void*thread_func(void*arg)
{
inti;
for(i=0;i<100000;i++){
pthread_mutex_lock(&mutex);
count++;
pthread_mutex_unlock(&mutex);
}
returnNULL;
}
intmain()
{
pthread_tt1,t2;
pthread_create(&t1,NULL,thread_func,NULL);
pthread_create(&t2,NULL,thread_func,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
printf("count=%d\n",count);
return0;
}
在上面的例子中,我们定义了一个互斥锁mutex和一个全局变量count,两个线程不断地对count进行累加操作,每次操作前都要先获得互斥锁,以保证线程安全性。
二、条件变量(ConditionVariable)
条件变量是一种高级的同步机制,它允许线程在某个条件满足时才进行操作。条件变量必须与互斥锁一起使用,以避免竞态条件。
下面是一个简单的使用条件变量的例子:
c
#include<stdio.h>
#include<pthread.h>
pthread_mutex_tmutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_tcond=PTHREAD_COND_INITIALIZER;
intcount=0;
void*producer(void*arg)
{
inti;
for(i=0;i<10;i++){
pthread_mutex_lock(&mutex);
count++;
printf("producer:count=%d\n",count);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
returnNULL;
}
void*consumer(void*arg)
{
while(1){
pthread_mutex_lock(&mutex);
while(count==0){
pthread_cond_wait(&cond,&mutex);
}
count--;
printf("consumer:count=%d\n",count);
pthread_mutex_unlock(&mutex);
}
}
intmain()
{
pthread_tt1,t2;
pthread_create(&t1,NULL,producer,NULL);
pthread_create(&t2,NULL,consumer,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
return0;
}
在上面的例子中,我们定义了一个互斥锁mutex和一个条件变量cond,一个线程不断地对count进行累加操作,并在每次操作后发送信号给条件变量,另一个线程在条件变量被满足时才进行count的减法操作。
三、读写锁(Read-WriteLock)
读写锁是一种特殊的锁,它允许多个线程同时读取共享资源,但只允许一个线程写入共享资源。这样可以大大提高程序的并发性能。
下面是一个简单的使用读写锁的例子:
c
#include<stdio.h>
#include<pthread.h>
pthread_rwlock_trwlock=PTHREAD_RWLOCK_INITIALIZER;
intcount=0;
void*reader(void*arg)
{
inti;
for(i=0;i<100000;i++){
pthread_rwlock_rdlock(&rwlock);
printf("reader:count=%d\n",count);
pthread_rwlock_unlock(&rwlock);
}
returnNULL;
}
void*writer(void*arg)
{
inti;
for(i=0;i<100000;i++){
pthread_rwlock_wrlock(&rwlock);
count++;
printf("writer:count=%d\n",count);
pthread_rwlock_unlock(&rwlock);
}
returnNULL;
}
intmain()
{
pthread_tt1,t2,t3,t4;
pthread_create(&t1,NULL,reader,NULL);
pthread_create(&t2,NULL,reader,NULL);
pthread_create(&t3,NULL,writer,NULL);
pthread_create(&t4,NULL,writer,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_join(t3,NULL);
pthread_join(t4,NULL);
return0;
}
在上面的例子中,我们定义了一个读写锁rwlock和一个全局变量count,两个读线程不断地读取count的值,两个写线程不断地对count进行累加操作。由于读写锁允许多个线程同时读取共享资源,因此读线程可以并发执行,提高了程序的效率。
四、信号量(Semaphore)
信号量是一种经典的同步机制,它可以用于控制对共享资源的访问。当一个线程需要访问共享资源时,它必须先获得一个信号量,如果信号量的值为0,则该线程将被阻塞,直到有其他线程释放了一个信号量。
下面是一个简单的使用信号量的例子:
c
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
sem_tsem;
intcount=0;
void*thread_func(void*arg)
{
inti;
for(i=0;i<100000;i++){
sem_wait(&sem);
count++;
printf("count=%d\n",count);
sem_post(&sem);
}
returnNULL;
}
intmain()
{
sem_init(&sem,0,1);
pthread_tt1,t2;
pthread_create(&t1,NULL,thread_func,NULL);
pthread_create(&t2,NULL,thread_func,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
sem_destroy(&sem);
return0;
}
在上面的例子中,我们定义了一个信号量sem和一个全局变量count,两个线程不断地对count进行累加操作,每次操作前都要先获得一个信号量。由于信号量只有一个许可证,因此同一时刻只有一个线程可以访问共享资源。
五、总结
本文介绍了Linux下常用的同步机制,包括互斥锁、条件变量、读写锁和信号量。不同的同步机制适用于不同的场景,读者应该根据实际情况选择合适的同步机制,以提高程序的效率和稳定性。
whatsapp最新版:https://cjge-manuscriptcentral.com/software/6928.html