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

concurrent_bounded_queue usage

zoolii
Beginner
911 Views
Hi All ,
I am trying to use concurrent_bounded_queue for a producer/cosumer problem.
I want to have a background thread to collect the garbage. But my code is not working because the blocking "pop()"
never returns even if another thread adds an item to the Queue.
Most probably this could be something to do with my code and understanding . I expect the "push()" to notify the waiting "pop()" thread. Is it so ?
I am posting my basic idea here. Please help.
Thank you.
Zooli

[bash]class GC
{

private :
	tbb::concurrent_bounded_queue nodequeue;

public :

	GC()
	{
	}
	
	void operator()()
	{
		while(true) {
		
			Item* pItem = 0;
			
			nodequeue.pop(pItem);
			
			delete pItem;
		
		}
	}

	void finalize(Item* pItem)
	{
		nodequeue.push(pItem);
		
	}

};

void main()
{
	GC gc;
	thread thread1(gc);
	......
/*Some other threads create and delete "Item" as main thread does below */
	......


	Item* pItem = create(); /* Main thread also create one item*/

	gc.finalize(pItem); /* Main thread adds "Item" for garbage collecton*/
	
	thread1.join();

};[/bash]
0 Kudos
1 Solution
Alexey-Kukanov
Employee
911 Views
Most likely, the problem is in passing gc into the thread.
In the C++1x draft, functions or function objects are passed into std::thread using rvalue references, i.e. withmoving semantics which implies that the object should not be used in the forking thread. Since rvalue references are not available in the current C++ standard, tbb::tbb_thread passes the functionby value instead. Therefore you get a copy of GC and a copy of the queue in your thread, and end up pushing into one queue instancebut popping from another one.

View solution in original post

0 Kudos
4 Replies
smasherprog
Beginner
911 Views
try switching to a concurrent_queue and use try_pop() and push() ... seeif that fixes it
0 Kudos
zoolii
Beginner
911 Views
Hi smasheprog, I need ablocking queue and that is why I opted for concurrent_bounded_queue. Also I do not want to spin myself to check an item is available or not. It seems like concurrent_bounded_queue promising that. Thank you.
0 Kudos
Alexey-Kukanov
Employee
912 Views
Most likely, the problem is in passing gc into the thread.
In the C++1x draft, functions or function objects are passed into std::thread using rvalue references, i.e. withmoving semantics which implies that the object should not be used in the forking thread. Since rvalue references are not available in the current C++ standard, tbb::tbb_thread passes the functionby value instead. Therefore you get a copy of GC and a copy of the queue in your thread, and end up pushing into one queue instancebut popping from another one.
0 Kudos
zoolii
Beginner
911 Views
Hi Alexy, Exactly that was the problem. Thanks a lot.
0 Kudos
Reply