- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am trying to parallelize an algorithm that accepts a callback and looks like this:
class Object; class MyCallback { public: virtual void NotifyAboutCompletion (Object* o) = 0; }; void Foo (Object* o, size_t n, MyCallback* c) { for (size_t i = 0; i < n; ++i) { DoSomeHeavyWork (o); c->NotifyAboutCompletion (o); } }
TBB pipeline should be quite usable to concurrently process objects and ensure the order. However I have a challenge how to meet the same thread guarantee.
The challenge is where to get that thread which would invoke the pipeline ? Creating an arbitrary thread (as shown in the example in the documentation) each time is expensive and can oversubscribe the machine - Foo() can be called any time from any place. To avoid oversubscription I thought to grab some TBB worker thread just to start the pipeline. AFAIU this could probably be done with tbb::task::enqueue(). However I am concerned that in some degenerated cases when there are no workers (explicit tbb::task_scheduler_init(1), or isolated arena or single CPU machine) the pipeline would never start and hence Foo() would never actually return.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I suspect the problem comes from a UI thread that must display some progress notification and redrawing widgets must be done from the main thread.
If this is the case, I would use an event that the main thread listen to. Once completed, your callback function would enqueue it's completed status somewhere and then post the event. Once posted, the main thread would wake up and dequeue status and update UI accordingly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello Michel,
Thank you for the response. Yes, this is what I am rather leaning to myself too.
The guarantee for Foo() will be relaxed and the MyCallback::NotifyAboutCompletion() may be called from any thread the Foo() will run in. And it will be a developer responsibility to make sure that redefined NotifyAboutCompletion() will post events which will be picked up later on by the main thread.
Thanks again,
Roman
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page