生产者和消费者 - 多线程
producer & consumer - multithreading
我写了一个这样的多线程程序,
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <pthread.h>
#include <unistd.h>
using namespace std;
pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t ec = PTHREAD_COND_INITIALIZER;
pthread_cond_t fc = PTHREAD_COND_INITIALIZER;
queue<int> qu;
const int N = 2;
void *producer(void *arg)
{
while(1) {
pthread_mutex_lock(&mu);
int tmp = rand();
qu.push(tmp);
pthread_cond_signal(&ec);
if ((int) qu.size() > N) {
pthread_cond_wait(&fc, &mu);
}
pthread_mutex_unlock(&mu);
}
}
void *consumer(void *arg)
{
while(1) {
pthread_mutex_lock(&mu);
if ((int) qu.size() < 1) {
pthread_cond_wait(&ec, &mu);
}
int tmp = qu.front();
qu.pop();
if ((int) qu.size() <= N) {
pthread_cond_signal(&fc);
}
pthread_mutex_unlock(&mu);
//sleep(1);
}
}
int main()
{
pthread_t tid;
pthread_create(&tid, NULL, producer, NULL);
for (int i = 0; i < N; i++) {
pthread_t tid;
pthread_create(&tid, NULL, consumer, NULL);
}
printf("all createdn");
sleep(600);
}
当qu.size()
大于N
时,producer
应停止生产,当小于N
时,producer
恢复生产。
最大的问题是,如果我删除consumer
中的sleep(1);
,程序将运行到segmentation fault
,如果我保留sleep(1);
,程序运行正常。
为什么?这是否意味着consumer
消耗太快?
错误的唤醒可能是原因。如果条件为true,则线程将继续,但如果线程继续,则不能假设条件为true。
pthread_cond_timedwait()或pthread-cond_wait()函数可能会出现虚假唤醒。由于pthread_cond_timedwait()或pthread-cond_wait()的返回并不意味着该谓词的值,因此应在返回时重新评估该谓词。
例如
if (qu.size() == 0) {
pthread_cond_wait(&ec, &mu);
}
应该成为
while (qu.size() == 0) {
pthread_cond_wait(&ec, &mu);
}
如果你保留了sleep(1)
调用,并且整个调用没有崩溃,你就是幸运的:)
尝试使用pthread_mutex_init()显式初始化互斥对象,否则pthread_mutex_lock()调用似乎会失败。
来自文档:
Errors
The pthread_mutex_lock() and pthread_mutex_trylock()
functions may fail if:
EINVAL: The value specified by mutex
does not refer to an initialized mutex object.
相关文章:
- 在C++中使用cURL和多线程
- 多线程双缓冲区
- 为什么我的多线程作业队列崩溃
- 在main()之外初始化std::vector会导致性能下降(多线程)
- 试图创建一个多线程程序来查找0-100000000之间的总素数
- 为什么一个向量上的多线程操作很慢
- 学习多线程C++:添加线程不会使执行速度更快,即使它看起来应该
- 全局变量 多读取器 一个写入器多线程安全?
- boost::文件系统::recursive_directory_iterator多线程安全
- 如何阻止TensorFlow的多线程
- 如何在多线程中正确使用unique_ptr进行多态性?
- 并发/多线程:是否可以以这种方式生成相同的输出?
- sigwait() 在多线程程序中不起作用
- 多线程程序中出现意外的内存泄漏
- 静态 constexpr 类成员变量对多线程读取是否安全?
- C++多线程生产者-消费者问题
- 多线程程序生产者/消费者[BOOST]
- 具有四个队列的多线程生产者/消费者
- 生产者和消费者 - 多线程
- 基于生产者-消费者的多线程图像处理