Community
cancel
Showing results for 
Search instead for 
Did you mean: 
zoolii
Beginner
73 Views

concurrent_bounded_queue usage

Jump to solution
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_K_Intel3
Employee
73 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

4 Replies
smasherprog
Beginner
73 Views
try switching to a concurrent_queue and use try_pop() and push() ... seeif that fixes it
zoolii
Beginner
73 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.
Alexey_K_Intel3
Employee
74 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

zoolii
Beginner
73 Views
Hi Alexy, Exactly that was the problem. Thanks a lot.
Reply