【发布时间】: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