C++ 使用sleep()时,生产者-消费者(使用监视器)代码不工作? #包括 #包括 #包括 #定义最大值10 使用名称空间std; 类边界缓冲区 { 私人: int缓冲区[MAX]; int填充,使用; 完整输入; pthread\u mutex\u t monitor;//监视器锁 pthread_cond_t为空; pthread_cond_t full; 公众: BoundedBuffer() { use=fill=fullEntries=0; } 无效生成(int元素) { pthread_mutex_lock(&monitor); while(fullEntries==MAX) pthread_cond_wait(&empty,&monitor); 缓冲区[填充]=元素; 填充=(填充+1)%MAX; fullEntries++; //睡眠(rand()%2); pthread_cond_信号(&full); pthread_mutex_unlock(&monitor); } 整数消耗() { pthread_mutex_lock(&monitor); while(fullEntries==0) pthread_cond_wait(&full,&monitor); int tmp=缓冲区[使用]; 使用=(使用+1)%MAX; 完整条目--; //睡眠(rand()%2); pthread_cond_信号(&empty); pthread_mutex_unlock(&monitor); 返回tmp; } }b; 无效*生产者(无效*参数){ int i=1; while(true){ b、 生产(一); i++; } } 无效*使用者(无效*参数){ while(true){ cout

C++ 使用sleep()时,生产者-消费者(使用监视器)代码不工作? #包括 #包括 #包括 #定义最大值10 使用名称空间std; 类边界缓冲区 { 私人: int缓冲区[MAX]; int填充,使用; 完整输入; pthread\u mutex\u t monitor;//监视器锁 pthread_cond_t为空; pthread_cond_t full; 公众: BoundedBuffer() { use=fill=fullEntries=0; } 无效生成(int元素) { pthread_mutex_lock(&monitor); while(fullEntries==MAX) pthread_cond_wait(&empty,&monitor); 缓冲区[填充]=元素; 填充=(填充+1)%MAX; fullEntries++; //睡眠(rand()%2); pthread_cond_信号(&full); pthread_mutex_unlock(&monitor); } 整数消耗() { pthread_mutex_lock(&monitor); while(fullEntries==0) pthread_cond_wait(&full,&monitor); int tmp=缓冲区[使用]; 使用=(使用+1)%MAX; 完整条目--; //睡眠(rand()%2); pthread_cond_信号(&empty); pthread_mutex_unlock(&monitor); 返回tmp; } }b; 无效*生产者(无效*参数){ int i=1; while(true){ b、 生产(一); i++; } } 无效*使用者(无效*参数){ while(true){ cout,c++,multithreading,operating-system,synchronization,ipc,C++,Multithreading,Operating System,Synchronization,Ipc,当我使用fflush(stdout)时,我看到输出正在打印 while(true){ 您尝试运行该程序多长时间了?您是否阅读了rand和sleep的文档。 兰德公司每次都会返回一个大数字,而你的制作人在第一次制作完某样东西后会长时间处于睡眠状态,而在这段时间里,你的消费者只是等待cond_wait 尝试使用较小的睡眠间隔“usleep”或类似的东西,你会发现你的程序运行得很好 这就是io操纵器的用途。@sonu kumar:如果上面解决了您的问题,您可以接受答案,这样其他人也可以受益!cou

当我使用fflush(stdout)时,我看到输出正在打印

while(true){

您尝试运行该程序多长时间了?
您是否阅读了rand和sleep的文档。

兰德公司每次都会返回一个大数字,而你的制作人在第一次制作完某样东西后会长时间处于睡眠状态,而在这段时间里,你的消费者只是等待cond_wait

尝试使用较小的睡眠间隔“usleep”或类似的东西,你会发现你的程序运行得很好


这就是io操纵器的用途。@sonu kumar:如果上面解决了您的问题,您可以接受答案,这样其他人也可以受益!cout@SimpleGuy我过去从未遇到过这种问题。你知道为什么会发生吗?@sonu kumar:
fflush
用于刷新缓冲区(stdout)在您的情况下。如果您过去没有遇到此问题,那么您的缓冲区在过去是自动刷新的。很多时候,它是自动刷新的(例如,可以在缓冲区已满的基础上推送一个条件).因此,在过去的情况下,顺便说一下,通常最好将信号置于临界区域之外。@kec-这不是真的。有关详细信息,请参阅why@Sean当前位置关于该问题的选定答案具有误导性。给出的初始案例根本没有锁定,这不是我所建议的。至于哪一个更好,我认为在非re中所有时间的应用程序,在关键部分之外更好。有关详细信息,请参阅该问题的第二个答案和引用的Google groups post。@Sean:也请参见:。我没有投反对票,但您的声明
可能会发生这样的情况,兰德每次都返回一个大数字
不可能是真的,因为它是
rand()%2
所以睡眠不能超过
0或1
不是我,但可能是因为注释行中有
%2
。我的错!我错过了。谢谢您的注释。我确实运行了它,将endl放在语句的末尾,它运行得很好。@Rush当我运行这个程序时,打印输出所花费的时间比应该的要多ut。无论如何,我没有投反对票。在加入fflush后,效果很好。
#include<bits/stdc++.h>
#include<pthread.h>
#include<unistd.h>
#define MAX 10
using namespace std;
class BoundedBuffer
{
private:
  int buffer[MAX];
  int fill, use;
  int fullEntries;
  pthread_mutex_t monitor;  // monitor lock
  pthread_cond_t empty;
  pthread_cond_t full;
public:
  BoundedBuffer ()
  {
    use = fill = fullEntries = 0;
  }
  void produce (int element)
  {
    pthread_mutex_lock (&monitor);
    while (fullEntries == MAX)
      pthread_cond_wait (&empty, &monitor);
    buffer[fill] = element;
    fill = (fill + 1) % MAX;
    fullEntries++;
    //sleep(rand()%2);
    pthread_cond_signal (&full);
    pthread_mutex_unlock (&monitor);
  }
  int consume ()
  {
    pthread_mutex_lock (&monitor);
    while (fullEntries == 0)
      pthread_cond_wait (&full, &monitor);
    int tmp = buffer[use];
    use = (use + 1) % MAX;
    fullEntries--;
    //sleep(rand()%2);
    pthread_cond_signal (&empty);
    pthread_mutex_unlock (&monitor);
    return tmp;
  }
}b;
void* producer(void *arg){
int i=1;
while(true){
b.produce(i);
i++;
}
}
void* consumer(void *arg){
while(true){
cout<<b.consume()<<" ";
}
}
int main(){
pthread_t t1,t2;
pthread_create(&t1,NULL,producer,NULL);
pthread_create(&t2,NULL,consumer,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
return 0;
}
while(true){
cout<<"["<<b.consume()<<"] ";
fflush(stdout)
}