Linux线程---线程同步互斥:信号量

时间:2022-04-01 15:14:25

从概念上来说,信号量是一个非负整数计数。信号量通常用来协调对资源的访问,其
中信号计数会初始化为可用资源的数目。然后,线程在资源增加时会增加计数,在删
除资源时会减小计数,这些操作都以原子方式执行。

如果信号计数变为零,则表明已无可用资源。计数为零时,尝试减小信号的线程会被
阻塞,直到计数大于零为止。

由于信号无需由同一个线程来获取和释放,因此信号可用于异步事件通知,如用于信
号处理程序中。同时,由于信号包含状态,因此可以异步方式使用,而不用像条件变
量那样要求获取互斥锁。但是,信号的效率不如互斥锁高。

缺省情况下,如果有多个线程正在等待信号,则解除阻塞的顺序是不确定的。
信号在使用前必须先初始化,但是信号没有属性。

调用时:
如果当做互斥锁的时候,一般初始化的时候初始化为1
如果需要进行同步的时候,一般初始化的时候信号量为0或可用资源数目

一个简单的例子
保证线程执行顺序为:线程1、线程2、线程3

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

sem_t sem_one, sem_two;

void onef()
{
    printf("t1 runing\n");

    // int sem_post(sem_t *sem);
    // 可以原子方式增加sem所指示的信号
    // 如果多个线程基于该信号阻塞,则系统会解除阻塞其中一个线程
    sem_post(&sem_one);
}

void twof()
{
    // int sema_wait(sema_t *sp);
    // 可以一直阻塞调用线程,直到sp所指向的信号的计数变得大于零为止
    // 计数变得大于零时,系统会以原子方式减小计数
    sem_wait(&sem_one);
    printf("t2 runing\n");
    sem_post(&sem_two);
}

void threef()
{
    sem_wait(&sem_two);
    printf("t3 runing\n");
}

int main()
{
    pthread_t one, two, three;

    // int sem_init(sem_t *sem, int pshared, unsigned int value);
    // 如果pshared的值为零,则不能在进程之间共享信号。
    // 如果pshared的值不为零,则可以在进程之间共享信号。
    // value为可用资源数
    sem_init(&sem_one, 0, 0);
    sem_init(&sem_two, 0, 0);

    pthread_create(&one, NULL, (void*)onef, NULL);
    pthread_create(&two, NULL, (void*)twof, NULL);
    pthread_create(&three, NULL, (void*)threef, NULL);

    pthread_join(one, NULL);
    pthread_join(two, NULL);
    pthread_join(three, NULL);

    sem_destroy(&sem_one);
    sem_destroy(&sem_two);

    return 0;
}

Linux线程---线程同步互斥:信号量

多个生产者、多个消费者

1).满足互斥与同步条件,用互斥锁和信号量实现

2).多个生产者和消费者:生产者和生产者属于互斥关系;生产者和消费者属于互斥和同步关系;消费者和消费者属于互斥关系

3).生产者和消费者模型中存在如下几种关系和角色:3种关系,2种角色,1种交换媒介(一般是一段内存)

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

pthread_mutex_t proMutex;
pthread_mutex_t conMutex;
sem_t occupied;
sem_t empty;

int count = 1;
const int maxCount = 5;

void producer()
{
    while(1)
    {
        sem_wait(&empty);
        pthread_mutex_lock(&proMutex);
        count = count + 1;
        printf("producer_%d: %d\n", (unsigned int)pthread_self(), count);
        pthread_mutex_unlock(&proMutex);        
        sem_post(&occupied);
        usleep(1000);
    }
}

void consumer()
{
    while(1)
    {
        sem_wait(&occupied);
        pthread_mutex_lock(&conMutex);
        count = count - 1;
        printf("consumer_%d: %d\n", (unsigned int)pthread_self(), count);
        pthread_mutex_unlock(&conMutex);    
        sem_post(&empty);
        usleep(1000);
    }
}

int main()
{
    pthread_t pro, pro1, con, con1;

    pthread_mutex_init(&proMutex, NULL);
    pthread_mutex_init(&conMutex, NULL);
    sem_init(&occupied, 0, 0);
    sem_init(&empty, 0, maxCount);

    pthread_create(&pro, NULL, (void*)producer, NULL);
    pthread_create(&pro1, NULL, (void*)producer, NULL);
    pthread_create(&con, NULL, (void*)consumer, NULL);
    pthread_create(&con1, NULL, (void*)consumer, NULL);

    pthread_join(pro, NULL);
    pthread_join(pro1, NULL);
    pthread_join(con, NULL);
    pthread_join(con1, NULL);   

    pthread_mutex_destroy(&proMutex);
    pthread_mutex_destroy(&conMutex);
    sem_destroy(&occupied);
    sem_destroy(&empty);

    return 0;
}

Linux线程---线程同步互斥:信号量

参考
多线程编程指南
https://blog.csdn.net/qq_29924041/article/details/69053518
https://blog.csdn.net/qq_34328833/article/details/56012780