【问题标题】:synchronization error in producer consumer using pthreads and semaphore使用 pthreads 和信号量的生产者消费者中的同步错误
【发布时间】:2016-12-27 14:41:32
【问题描述】:

我使用 pthread 和信号量编写了一个简单的生产者消费者。我有时会出现故障(消费者在生产者生产之前消费)输出。请帮我找出问题。我已经使用各种资源和教程验证了逻辑,但仍然得到不希望的结果。

    #include <iostream>
    #include <pthread.h>
    #include <semaphore.h>
    #include <string>
    #include <sstream>
    #include <unistd.h>
    #include "pthread_barrier.hpp"

    sem_t empty;
    sem_t full;
    sem_t lock;
    pthread_mutex_t wlock;

    pthread_barrier_t pbarrier;
    pthread_barrier_t cbarrier;

    pthread_attr_t tattr;

    #define BUFF_SIZE 100

    volatile bool buffer[BUFF_SIZE];
    int prodIterator = 0;
    int consIterator = 0;

    void *Producer(void *args)
    {   
        pthread_barrier_wait(&pbarrier);
        while(1) {
            sem_wait(&empty);
            sem_wait(&lock);
            buffer[prodIterator] = true;
            pthread_mutex_lock(&wlock);
            std::stringstream str;
            std::cout<<"producer produced = "<<prodIterator<<"\n";
            pthread_mutex_unlock(&wlock);
            prodIterator = (++prodIterator)% BUFF_SIZE;
            sem_post(&lock);
            sem_post(&full);
            sleep(1);
        }
    }

    void *Consumer(void *args)
    {
        pthread_barrier_wait(&cbarrier);
        while(1) {
            sem_wait(&full);
            sem_wait(&lock);
            buffer[consIterator] = false;
            pthread_mutex_lock(&wlock);
            std::cout<<"Consumer consumed = "<<consIterator<<"\n";
            pthread_mutex_unlock(&wlock);
            consIterator = (++consIterator)% BUFF_SIZE;
            sem_post(&lock);
            sem_post(&empty);
            sleep(1);
        }   
    }


    int main()
    {
        sem_init(&empty, 0, BUFF_SIZE);
        sem_init(&full, 0, 0);
        sem_init(&lock, 0, 1);

        pthread_mutex_init(&wlock, NULL);

        pthread_t prod[10];
        pthread_t cons[10];

        unsigned pcount = 5;
        unsigned ccount = 2;
        pthread_barrier_init(&pbarrier, NULL, pcount);
        pthread_barrier_init(&cbarrier, NULL, ccount);

        pthread_attr_init(&tattr);
        pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);

        for(int i=0;i<5;i++) {
            pthread_create(&prod[i], &tattr, Producer, NULL);
        }

        for(int i=0;i<2;i++) {
            pthread_create(&cons[i], &tattr, Consumer, NULL);
        }

        pthread_exit(NULL);
    }

【问题讨论】:

  • 什么是 pthread_barrier?
  • pthread_barrier 用于停止线程执行,直到确定数量的线程命中 pthread_barrier_wait api。它只是为了创建真正的竞争条件和多线程环境。

标签: c++ synchronization pthreads semaphore producer-consumer


【解决方案1】:

我发现了问题所在。我正在使用 mac osx,而 mac osx 不支持 unnmaed emaphores。它只支持命名信号量。因此,为了代替在我的代码中使用未命名的信号量,我需要用命名信号量替换它以使代码工作。 OSX 对 pthread 的支持很差。它也不支持 pthread 屏障。 所以我需要用以下代码替换信号量初始化行:

           full = sem_open("/semaphore1", O_CREAT, 0777, 1);
           empty = sem_open("/semaphore2", O_CREAT, 0777, 1);
           lock = sem_open("/semaphore3", O_CREAT, 0777, 1);

           and the declaration of semaphore variables needed be replace by :

           sem_t *empty;
           sem_t *full;
           sem_t *lock;

           very poor support indeed for pthreads in mac osx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    相关资源
    最近更新 更多