【问题标题】:Seg fault in a producer consumer program生产者消费者程序中的段错误
【发布时间】:2020-03-09 03:00:02
【问题描述】:

我通过实现生产者消费者问题来学习并发编程。代码只有一个生产者和一个消费者。消费者线程坐在一个被空信号量阻塞的while循环中。该代码生成我无法调试的段错误 11。我尝试删除消费者线程中的while循环,之后我看不到段错误为什么会这样?

#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <queue>
#include <unistd.h>
#include <semaphore.h>
using namespace std;

queue<int> buff;
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
sem_t empty;

void producer(int i){

   pthread_mutex_lock(&mut);
   buff.push(i);
   sem_post(&empty);
   pthread_mutex_unlock(&mut);

}

void* produce_data(void* arg){
    for(int i = 5 ; i < 10 ; i++){
        producer(i);
    }

   return NULL;
}

void* consumer(void* arg){

    while(1){
        sem_wait(&empty);
        pthread_mutex_lock(&mut);
        int temp = buff.front();
        cout << temp <<" " ;
        buff.pop();
        pthread_mutex_unlock(&mut);
     }

    return 0;
}

int main () {
   pthread_t prod;
   sem_init(&empty, 0, 0);
   pthread_create(&prod, NULL, produce_data , NULL);

   pthread_t consum2;

   pthread_create(&consum2, NULL,consumer , NULL);

   pthread_join(prod,NULL);

   pthread_join(consum2,NULL);


   return 0;

}

【问题讨论】:

  • 你能准确解释一下是什么让你“无法调试”这个简单的程序吗?
  • sem_post移到互斥体之外。
  • 我试图打印一些东西但没有帮助。就像在消费者中我所做的只是弹出队列然后在信号量上休眠,我不确定如何在这里访问非法内存
  • @stark 没有帮助将 sem_post 放在外面
  • 无法重现。添加代码以刷新cout 会导致预期的输出,然后是挂起,因为一个线程在pthread_join 中,而另一个线程在无限的while 循环中。

标签: c++ multithreading concurrency posix


【解决方案1】:

Sem_init() 在 macos 中已弃用。编译你的程序应该已经显示了这一点。此外,如果您检查返回值,您会知道 sem_init()、sem_post()、sem_wait() 都失败了。无论出于何种原因,大约 1023 会从您的队列中弹出,并且是 SEGV。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多