我正在使用concurrent_bounded_queue
Intel TBB 4.1 Update 3 进行生产者和消费者线程之间的通信:
队列类有一个调用方法,该方法abort
会抛出tbb::user_abort
阻塞在队列实例上pop
和push
队列实例上的所有线程。两个线程之间的通信可能如下所示:
ConsThread | ProdThread
-----------+-------------
q.pop | get new data
(wait) | q.push
process | get new data
q.pop | no more data!
(wait) | q.abort
quit | quit
不幸的是,即使在这个简单的示例中,我也无法使用它来可靠地关闭队列,因为如果某些消费者pop
在调用之前没有完成对先前 ped 数据的处理abort
,他们将完成迭代并返回阻塞pop
:
ConsThread | ProdThread
-----------+-------------
q.pop | get new data
(wait) | q.push
process | get new data
process | no more data!
process | q.abort
process | quit
process |
q.pop |
(wait) |
(wait) |
(wait) |
(so lonely)|
现在我正在使用一个中度恶心的 hack,它产生另一个非分离线程(它加入消费者池线程)并等待它完成,同时abort
不时为后来者发送更多 s:
bool areConsumerThreadsJoinedThankYou = false;
std::thread joiner(Joiner(consumerPool, &areConsumerThreadsJoinedThankYou));
while (!areConsumerThreadsJoinedThankYou) {
rawQueue.abort();
MAGIC_MSLEEP(100);
}
class Joiner
的实现差不多
void Joiner::operator()(void)
{
for (auto it = this->m_threadPool.begin();
it < this->m_threadPool.end();
it++)
(*it)->join();
this->m_done = true;
*(this->m_flag) = true;
}
这当然是非常丑陋的。有没有更根本的解决方案?