- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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]
1 Solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
try switching to a concurrent_queue and use try_pop() and push() ... seeif that fixes it
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Alexy, Exactly that was the problem. Thanks a lot.
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page