Linux Posix信号量 读写锁

本文介绍了Linux下使用POSIX信号量进行同步操作,包括`sem_init()`、`sem_wait()`和`sem_post()`等函数的用法。同时,讲解了读写锁的概念,适合读多写少的场景,强调写操作的独占性和优先级。通过`pthread_rwlock_t`创建和初始化读写锁,使用`pthread_rwlock_rdlock()`和`pthread_rwlock_wrlock()`进行读写加锁,`pthread_rwlock_unlock()`解锁,最后用`pthread_rwlock_destroy()`销毁读写锁。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

POSIX信号量

sem_t

sem_init()

int sem_init(sem_t *sem, int pshared, unsigned int value);

参数:pshared:0表⽰示线程间共享,⾮非零表⽰示进程间共享 value:信号量初始值

sem_wait() 条件不满足,等待

sem_post() 通知

sem_destroy()

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

sem_t producer;//notify producer 
sem_t consumer;//notify consumer
int noodles = 1;

void *thr_producer(void *arg)
{
    while(1)
    {
        sem_wait(&producer);
        printf("make noodles\n");
        noodles = 1;
        sleep(1);
        sem_post(&consumer);
    }
    return NULL;
}

void *thr_consumer(void *arg)
{
    while(1)
    {
        sem_wait(&consumer);
        printf("get noodles\n");
        noodles = 0;
        sleep(1);
        sem_post(&producer);
    }
    return NULL;
}
int main()
{
    pthread_t tid1,tid2;
    sem_init(&producer, 0, 0);//0:
    sem_init(&consumer, 0, 1);//0:一开始有一包面,在上面函数中就不用判断了

    pthread_create(&tid1,NULL,thr_producer,NULL);
    pthread_create(&tid2,NULL,thr_consumer,NULL);

    pthread_join(&tid1,NULL);
    pthread_join(&tid2,NULL);
    sem_destroy(&producer);
    sem_destroy(&consumer);
    return 0;
}

读写锁

多应用于读多写少的情景
写独占,读共享,写锁优先级高

创建一个读写锁:pthread_rwlock_t rwlock;

初始化读写锁:pthread_rwlock_init(&rwlock,NULL);

加锁:
- pthread_rwlock_wrlock(&rwlock);
- pthread_rwlock_rdlock(&rwlock);

解锁:pthread_rwlock_unlock(&rwlock);

销毁:pthread_rwlock_destroy(&rwlock);

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

char *ptr = "i am blackboard";

pthread_rwlock_t rwlock;

void *thr_write(void *arg)
{
    while(1)
    {
        pthread_rwlock_wrlock(&rwlock);
        printf("draw blackboard\n");
        sleep(2);
        pthread_rwlock_unlock(&rwlock);
        usleep(1000);
    }
    return NULL;
}

void *thr_read(void *arg)
{
    int id = (int)arg;
    while(1)
    {
        pthread_rwlock_rdlock(&rwlock);
        sleep(1);
        printf("public %d:read blackboard [%s]\n",id,ptr);
        pthread_rwlock_unlock(&rwlock);
        usleep(1000);
    }
    return NULL;
}

int main()
{
    pthread_t tid1,tid2[2];
    pthread_rwlock_init(&rwlock,NULL);

    pthread_create(&tid1,NULL,thr_write,(void *)1);
    pthread_create(&tid2[0],NULL,thr_read,(void *)2);
    pthread_create(&tid2[1],NULL,thr_read,(void *)3);

    pthread_join(&tid1,NULL);
    pthread_join(&tid2[0],NULL);
    pthread_join(&tid2[1],NULL);

    pthread_rwlock_destroy(&rwlock);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值