两者在定义上的区别
- 信号量是用在多线程同步上的,一个线程完成了某一个动作就通过信号量告诉别的线程,别的线程就可以进行某些动作,如果所有的线程都在(sem_wait),就会阻塞。
- 互斥锁是用在多线程的互斥的,对于临界资源的访问,需要互斥进行。也就是说当一个线程占用了一个资源的时候,别的线程就无法访问,直到这个互斥锁被打开。通常锁和信号量是同时使用的,我们会在接下来的例子中给大家进行演示。
- 互斥量的加锁和解锁必须由同一线程分别对应使用,信号量可以由一个线程释放,另一个线程得到。
- 互斥锁(Mutex)保证了使用资源线程的唯一性和排他性,但是无法限制资源释放后其他线程申请的顺序问题,所以是无序的。
- 信号量(Semaphore)一般就是互斥的(有些读的资源是可以同时进行分配的),其保证了线程执行的有序性。
两者在语义上的区别
- 信号量的的value>=0
- 互斥锁的valude可以为负数
信号量实现同步
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<assert.h>
#include<pthread.h>
#include<semaphore.h>
sem_t sem;
void *thread_fun(void *arg)
{
int i=0;
for(;i<5;i++)
{
sem_wait(&sem);
write(1,"B",1);
int n=rand()%3;
sleep(n);
write(1,"B",1);
sem_post(&sem);
n=rand()%3;
sleep(n);
}
}
int main()
{
pthread_t id;
sem_init(&sem,0,1);
int i=0;
for(;i<5;i++)
{
sem_wait(&sem);
write(1,"A",1);
int n=rand()%3;
sleep(n);
write(1,"A",1);
sem_post(&sem);
n=rand()%3;
sleep(n);
}
pthread_join(id,NULL);
sem_destroy(&sem);
printf("\n");
exit(0);
}

互斥锁实现同步
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<assert.h>
#include<pthread.h>
#include<semaphore.h>
pthread_mutex_t mutex;
void *thread_fun(void *arg)
{
int i=0;
for(;i<5;i++)
{
pthread_mutex_lock(&mutex);
write(1,"B",1);
int n=rand()%3;
sleep(n);
write(1,"B",1);
pthread_mutex_unlock(&mutex);
n=rand()%3;
sleep(n);
}
}
int main()
{
pthread_t id;
pthread_mutex_init(&mutex,NULL);
pthread_create(&id,NULL,thread_fun,NULL);
int i=0;
for(;i<5;i++)
{
pthread_mutex_lock(&mutex);
write(1,"A",1);
int n=rand()%3;
sleep(n);
write(1,"A",1);
pthread_mutex_unlock(&mutex);
n=rand()%3;
sleep(n);
}
pthread_join(id,NULL);
pthread_mutex_destroy(&mutex);
printf("\n");
exit(0);
}
条件变量实现同步
条件变量满足之后唤醒其他线程加锁
- 排除正在入队的情况,如果有线程正在进入等待条件变量的队列时,那个线程拿着互斥锁,如果主线程要唤醒,就会阻塞。如果主线程能拿到锁,表示等待条件变量的所有线程都已经进入等待队列。
- 锁是为了保证入队和出队的原子操作
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<assert.h>
#include<pthread.h>
#include<semaphore.h>
pthread_cond_t cond;
pthread_mutex_t mutex;
void *fun1(void *arg)
{
char *s=(char*)arg;
while(1)
{
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond,&mutex);
pthread_mutex_unlock(&mutex);
if(strncmp(s,"end",3)==0)
{
break;
}
printf("fun1:%s\n",s);
}
printf("fun1 over\n");
}
void *fun2(void *arg)
{
char *s=(char*)arg;
while(1)
{
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond,&mutex);
pthread_mutex_unlock(&mutex);
if(strncmp(s,"end",3)==0)
{
break;
}
printf("fun2:%s\n",s);
}
printf("fun2 over\n");
}
int main()
{
pthread_t id[2];
char buff[128]={0};
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(&cond,NULL);
pthread_create(&id[0],NULL,fun1,(void *)buff);
pthread_create(&id[1],NULL,fun2,(void *)buff);
while(1)
{
char readline[128]={0};
fgets(readline,128,stdin);
strcpy(buff,readline);
if(strncmp(readline,"end",3)==0)
{
break;
}
pthread_mutex_lock(&mutex);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_mutex_lock(&mutex);
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mutex);
pthread_join(id[0],NULL);
pthread_join(id[1],NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
exit(0);
}
