2

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...

3
  • Update your post, don't add details in comments. Commented Apr 6, 2017 at 13:19
  • Boost.Thread implements Futures with Continuations as an extension, including 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. Commented Apr 6, 2017 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) Commented Apr 6, 2017 at 13:55

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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()
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.
Here is a threaded queue of tasks: stackoverflow.com/a/30180853/1774667 I probably have a threaded_queue<T> somewhere on stackoverflow.
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.

3 Comments

Is there any good instructions on how to implement one, with OS facilities?
@Adam Working instructions in the form of the source code: sourceware.org/git/?p=glibc.git;a=tree;f=nptl
That's glibc and pthread source, and could u give me some extra articles or books to read about?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.