0

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?

slashmais
  • 7,069
  • 9
  • 54
  • 80
  • I think you may be able to do this using thread pools (like http://threadpool.sourceforge.net/) ? Not 100% sure it fits though – C4stor Oct 08 '13 at 09:33
  • 1
    I am not really sure what you want, but maybe you want to have a look at futures and promises. – PlasmaHH Oct 08 '13 at 09:35
  • well just use mutex, locks and that it is... it depends how you are accessing the data which are shared and so on... – Dusan Plavak Oct 08 '13 at 09:39
  • @PlasmaHH: you should put that in an answer - I found http://stackoverflow.com/questions/12620186/futures-vs-promises and http://stackoverflow.com/questions/11004273/what-is-stdpromise/12335206#12335206 - this is new to me & looks promising :) indeed! thx – slashmais Oct 08 '13 at 10:11

1 Answers1

1

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)

Community
  • 1
  • 1
PlasmaHH
  • 15,673
  • 5
  • 44
  • 57