Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
2466 Discussions

The proper way to cancel a ConcurrentQueue's Pop condition and his consumer thread

poetinha
Beginner
459 Views
Hi folks,

I'm currently migrating to TBB, anyway, I will have the same question... I've attached my sources to explain.

My consumer thread could be waiting on a queue.pop(), since it should be signaled on concurrent queue only... what's the better way to deal with this? I know that a queue will never have a terminate() method :P it's just a workaround for now.

Thanks in advance,

William

concurrent_queue-inl.h

test_consumer.h
0 Kudos
5 Replies
nagy
New Contributor I
459 Views
Something like this maybe?
void terminate()
{
queue.push(nullptr);
}
void run()
{
History* hist = queue.pop();
bool terminated = hist == nullptr;
}
0 Kudos
poetinha
Beginner
459 Views
pushing a NULL value works only on a blockingqueue, right?
0 Kudos
nagy
New Contributor I
459 Views
I dont understand? pop() is blocking in all tbbs concurrent queues, try_pop is non-blocking. And why would it only work on a blockingqueue?
0 Kudos
poetinha
Beginner
459 Views
My mistake. Thanks a lot. It solved my problem.
0 Kudos
Dmitry_Vyukov
Valued Contributor I
459 Views
Quoting poetinha
My mistake. Thanks a lot. It solved my problem.

Be careful, some use patterns can corrupt memory/crash:

http://software.intel.com/en-us/forums/showthread.php?t=73928&o=d&s=lr

0 Kudos
Reply