- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
#include "tbb/compat/thread" #include#include tbb::spin_mutex my_mutex; using namespace tbb::flow; using namespace tbb; struct Body { void operator()(const tbb::blocked_range &r) const { { tbb::spin_mutex::scoped_lock lock(my_mutex); cout<<"ThreadNr in 'Body': "<<:THIS_TBB_THREAD::GET_ID>(0, 2), Body()); return 0; } }; int testmain() { cout<<"##########parallel_for only:###########"< (0, 2), Body()); cout<<"##########parallel_for within flow_graph nodes###########"< node(g,node_body()); node.try_put(continue_msg()); g.wait_for_all(); cout<<"##########parallel_for only:###########"< (0, 2), Body()); return 0; }
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
tbb::task_scheduler_init does apply to flow graph scheduling, but just not when you ask for 1 thread :) Unlike the other TBB algorithms, the flow graph does not use tbb::spawn for its tasks, but instead tbb::enqueue. Tasks that are enqueued are (1) executed in roughly first-in-first-out order and (2) are starvation resistant. If a task is enqueued and there are currently no worker threads available in the thread pool to execute it, a new thread is temporarily added to the thread pool to execute the task. So, when your code does the try_put, this enqueues a task to execute Body, but theres no thread available to execute this task (the main thread is busy doing the try_put). So a new thread is temporarily added to the thread pool. Hence you have 2 threads executing your graph.
If you were running on a larger system, say with 8 cores, and you created a task_scheduler_init init(2), you would see only 2 threads. But when asking for 1 thread, the library creates a thread to service your task so that it doesnt starve. So, unfortunately that means that there is no (easy) way to run a graph on only a single thread.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- tbb::task_scheduler_init(1)
- graph::run(sleeping_body()), withsleeping_body::operator()(){ sleep(10000000000.0); }
- Start your graph: (for all) receiver.try_put()
- graph::decrement_wait_count()
- sleep(0.1) - this is used to ensure that the sleeping task is definitely run by the unwanted worker thread
- graph::wait_for_all()
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page