C++ 带信号量的有界MPMC队列
我在中创建了一个非常优秀的有界MPMC队列源代码 而且测试非常简单:C++ 带信号量的有界MPMC队列,c++,c,multithreading,queue,semaphore,C++,C,Multithreading,Queue,Semaphore,我在中创建了一个非常优秀的有界MPMC队列源代码 而且测试非常简单: mpmc_bounded_queue<string> mq(8) ; for(int idx=0;idx<10;idx++) { string s = to_string(idx); if(mq.enqueue(s)) cout << s << " ok" << endl ; else cout <<
mpmc_bounded_queue<string> mq(8) ;
for(int idx=0;idx<10;idx++)
{
string s = to_string(idx);
if(mq.enqueue(s))
cout << s << " ok" << endl ;
else
cout << s << " not ok" << endl ;
}
string strx;
if(mq.dequeue(strx))
{
cout << "The dequeue value=" << strx << endl ;
}else{
assert(1 == 2) ;
}
string s = "THE END" ;
if(mq.enqueue(s))
cout << s << " ok" << endl ;
else
cout << s << " not ok" << endl ;
我的观点是,线程C awaked意味着有新的数据排队,但它可能不在缓冲区[dequeue_pos_]的位置,因此将dequeue函数放在无止境循环中
要有帮助
这个工具我说得对吗?我的意思是,缓冲区[1]有可能被填满
在缓冲区[0]被唤醒且线程C尝试退出缓冲区[0]失败之前,请保持退出
在无止境的循环中,不会永远等待……因为当线程通过
操作系统,缓冲区[0]填充完毕,线程C将中断循环
任何建议,评论都将不胜感激
Right !!
while(1){
sem_wait(sem1) ;
while(1){
if(mq.dequeue(strx))
break ;
}
//doing something about strx
}
=====================================
Wrong !!
while(1){
sem_wait(sem1) ;
mq.dequeue(strx) ;
//doing something about strx
}
=====================================