1

Is there a decent wait_any implementation using c++11's concurrency primitives?

or how to implement it in c++11's mutex, condition_variable, ... ?

What is the general idea and algorithms in implementing it, with not only c++11, but also native Linux system call and pthread?

The wait_any is waiting any futures in a vector/array to be available, or any of multiple condition_variables to be signaled, and etc...

Adam
  • 1,684
  • 1
  • 19
  • 39
  • Update your post, don't add details in comments. – Kerrek SB Apr 06 '17 at 13:19
  • Boost.Thread implements Futures with Continuations as an extension, including [when_any](http://www.boost.org/doc/libs/1_63_0/doc/html/thread/synchronization.html#thread.synchronization.futures.reference.when_any). Don't know whether this satisfies your notion of 'decent', but it is a comparatively well-tested implementation. This implementation however is not strictly using C++11 primitives only, hence only a comment. – ComicSansMS Apr 06 '17 at 13:50
  • @ComicSansMS Well, depending on boost is sometimes too heavy. Is there any good instructions on how to implement one? (even with native linux API is fine) – Adam Apr 06 '17 at 13:55

2 Answers2

1

You can only have polling (busy-wait) implementations of mutex and condition variables if they do not use OS facilities to deschedule the waiting thread off the CPU.

Also, without the OS involvement there can be no such things as robust mutex or priority inversion mitigation.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
1

when_any does not exist in C++ std.

Usually I end up using a producer-consumer queue of messages. Any of the suppiers can queue a message in the queue (like "I am ready").

Many threads waiting on many such queues ends up being very complex.

In effect, you build things out of the C++ std primitives, you don't directly use them in client code.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • I actually like the `queue` stuff very much, and I can put an index in the queue, and access the data via index in vector, to use future.get() – Adam Apr 06 '17 at 14:20
  • Very practical, and I'd like to recommend this answer. However, I'd like to learn to get this done, if there's any instructions or articles or books, please notify me. – Adam Apr 06 '17 at 14:22
  • 1
    Here is a threaded queue of tasks: http://stackoverflow.com/a/30180853/1774667 I probably have a `threaded_queue` somewhere on stackoverflow. – Yakk - Adam Nevraumont Apr 06 '17 at 14:25
  • Aha, [`threaded_queue`](http://stackoverflow.com/a/42288975/1774667). – Yakk - Adam Nevraumont Apr 06 '17 at 16:00