Linux下的线程同步方式

目录

线程的同步方式

信号量

互斥锁

读写锁

条件变量

关于同步方式常见的问题

为什么有了互斥锁还要用信号量

互斥锁和读写锁的区别


线程的同步方式

信号量

信号量:一般情况下,如果只取0和1,将称之为二值信号量(代表可用或不可用)。如果信号量的值大于1,则称之为技术信号量(代表允许访问的资源数目,访问一个就减少一个(P操作),访问完成之后再加一(V操作),如同商场的试衣间一样)

我们这里使用的就是二值信号量

代码实现:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<assert.h>
#include<pthread.h>
#include<semaphore.h>


sem_t sem;
int val=1;
void* fun(void *arg)
{
    int i=0;
    for(;i<1000;++i)
    {
        sem_wait(&sem);
        printf("index=%d\n",val++);
        sem_post(&sem);
    }
}
int main()
{
    pthread_t id[5];
    sem_init(&sem,0,1);
    int i=0;
    for(;i<5;++i)
    {
        pthread_create(&id[i],NULL,fun,NULL);
    }
    for(i=0;i<5;++i)
    {
        pthread_join(id[i],NULL);
    }
    sem_destroy(&sem);
    exit(0);
}

互斥锁

互斥锁就是在多线程中,当其中某一线程需要对关键代码访问时,需要先获得一个锁,等访问完代码之后再释放这个锁,防止同一时间其它线程访问这段代码。

代码实现:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include<pthread.h>



pthread_mutex_t mutex;
int val=1;

void* fun(void* arg)
{
    for(int i=0;i<1000;++i)
    {
        pthread_mutex_lock(&mutex);
        printf("index=%d\n",val++);
        pthread_mutex_unlock(&mutex);
    }
}

int main()
{
    pthread_t id[5];
    pthread_mutex_init(&mutex,NULL);
    int i=0;
    for(;i<5;++i)
    {
        pthread_create(&id[i],NULL,fun,NULL);
    }
    for(i=0;i<5;++i)
    {
        pthread_join(id[i],NULL);
    }

    pthread_mutex_destroy(&mutex);
    exit(0);
}

读写锁

这里fun1和fun2是读,fun3是写,多个线程都读时,不需要加锁

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<pthread.h>




pthread_rwlock_t rwlock;

void* fun1(void* arg)
{
    while(1)
    {
        pthread_rwlock_rdlock(&rwlock);
        printf("fun1 read...start\n");
        sleep(1);
        printf("fun1 read..end\n");
        pthread_rwlock_unlock(&rwlock);
        int n=rand()%3;
        sleep(n);
    }
}

void* fun2(void* arg)
{
    while(1)
    {
        pthread_rwlock_rdlock(&rwlock);
        printf("fun2 read...start\n");
        sleep(1);
        printf("fun1 read..end\n");
        pthread_rwlock_unlock(&rwlock);
        int n=rand()%3;
        sleep(n);
    }
}

void* fun3(void*arg)
{
    int i=0;
    while(i<5)
    {
        pthread_rwlock_wrlock(&rwlock);
        printf("fun3 write start\n");
        sleep(1);
        printf("fun3 write end\n");
        pthread_rwlock_unlock(&rwlock);
        int n=rand()%3;
        sleep(n);
        ++i;
    }
}

int main()
{
    pthread_rwlock_init(&rwlock,NULL);
    pthread_t id1,id2,id3;
    pthread_create(&id1,NULL,fun1,NULL);
    pthread_create(&id2,NULL,fun2,NULL);
    pthread_create(&id3,NULL,fun3,NULL);


    pthread_join(id1,NULL);
    pthread_join(id2,NULL);
    pthread_join(id3,NULL);

    pthread_rwlock_destroy(&rwlock);
}

条件变量

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<pthread.h>


pthread_mutex_t mutex;
pthread_cond_t cond;

void* fun1(void* arg)
{
    char *s=(char*)arg;
    if(s==NULL)
    {
        pthread_exit(NULL);
    }
    while(1)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond,&mutex);
        pthread_mutex_unlock(&mutex);

        if(strncmp(s,"end",3)==0)
        {
            break;
        }
        printf("fun1 read:%s\n",s);
    }
}

void* fun2(void*arg)
{
    char *s=(char*)arg;
    if(s==NULL)
    {
        pthread_exit(NULL);
    }
    while(1)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond,&mutex);
        pthread_mutex_unlock(&mutex);

        if(strncmp(s,"end",3)==0)
        {
            break;
        }
        printf("fun2 read:%s\n",s);
    }
}

int main()
{
    pthread_mutex_init(&mutex,NULL);
    pthread_cond_init(&cond,NULL);


    char buff[128]={0};
    pthread_t id1,id2;
    pthread_create(&id1,NULL,fun1,buff);
    pthread_create(&id2,NULL,fun2,buff);

    while(1)
    {
        fgets(buff,128,stdin);
        if(strncmp(buff,"end",3)==0)
        {
            pthread_mutex_lock(&mutex);
            pthread_cond_broadcast(&cond);
            pthread_mutex_unlock(&mutex);
            break;
        }
        else
        {
            pthread_mutex_lock(&mutex);
            pthread_cond_signal(&cond);
            pthread_mutex_unlock(&mutex);
        }
    }

    pthread_join(id1,NULL);
    pthread_join(id2,NULL);
    exit(0);

}

对条件变量不懂的,可以看看这篇文章:

https://blog.csdn.net/qq_39736982/article/details/82380689

关于同步方式常见的问题

为什么有了互斥锁还要用信号量

互斥锁只能用于互斥性场景,当信号量只有一个,即只取0和1时,二者可以互换,其他情况下只能用信号量

互斥锁和读写锁的区别

假设有p1,p2,p3三个线程,如果使用互斥锁,则必须等待上一个线程操作完,才能进行下一个线程的操作;如果使用读写锁,就可以选择加读锁或者写锁,当加读锁时,可以让读操作的线程同时进行读取,当加写锁时,则和互斥锁一致。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值