Reputation: 7155
I am developing a (C++) program that during execution of a query may use multiple threads, some of which will be dependent on other threads started by itself or another thread within this query-process. Determining if a specific thread exists depends on specific parameters passed to the thread and this is part of thread-data in a list of active threads. Individual threads may complete quickly while others may take a long time.
To prevent trying to reinvent the wheel, is there some algorithm, pattern or best-practice to handle marshaling and waiting on multiple interdependent threads?
Upvotes: 0
Views: 162
Reputation: 16046
You might want to have a look at futures and promises, and at the async wrapper interface in C++11.
Promises allow you to deliver a result.
Futures allow you to wait for a result.
In this way you can express a whole lot of dependencies of computation among threads, and since every consumer waits properly for its producer (which may themselves be consumers again) for a lot of cases the dependencies work out themselves kind of automatically, if you take care.
More info:
A question about futures & promises
A very good explanation
A blog-entry about "broken promises" (- Bartosz Milewski)
Upvotes: 1