Repository navigation
Conversation
[why] No good reason, except that this follows our style guide and it makes the code bases more consistent.
This commit adds the ThreadSafeQueue class template. The original source code is taken from the Taskolib project: https://github.com/taskolib/taskolib The original class was called LockedQueue. I have adapted minor and obvious things like the namespace, include guards, and a part of the test suite.
[why] Marcus is one of the original authors of Taskolib's LockedQueue.
[why] To match the license of GUL17. [how] All original authors have consented, so I have added the usual MIT Doxygen header to the files.
[why]
A queue with zero entries does not make sense.
[how]
Throw std::invalid_argument when constructing ThreadSafeQueue{ 0 }.
[why] The class already was non-copyable and non-movable before because of its mutex and cv members. [how] Document non-copy/movability in the class documentation and explicitly delete all special member functions (rule of five).
[why] In several cases, wait() with a predicate is defined to execute exactly the check that we explicitly wrote out in the preceding if condition. This makes the if redundant.
[why] If left to their own devices, threads might still be blocked on pop() while the application is shutting down. [how] Add an explanation to the class documentation that it is in the user's responsibility to shut down all threads cleanly.
[why] back() consumes a "message available" notification, but actually does not remove a message from the queue. If we use notify_one() for the notification, another waiting thread does not get the notification. [how] Use notify_all() to issue the notification. Also, document the behavior of the various push() and pop() functions in case of exceptions.
[why] What we constructed before by moving from the queue was a T, not an optional<T>. This made it somewhat inefficient to return it from the function. [how] Directly construct an optional<T> from the moved queue element. This is then a candidate for NRVO.
[why] We use notify_one() to notify push() waiters of a free slot. If the push_back() throws, the notification has been consumed but the slot is still free. We should notify the next waiting thread, if any. [how] Catch exceptions thrown by push_back(). If one occurs, re-issue the notification and rethrow the exception.
|
This looks good, will have a look later. The only thing that comes to my mind after skimming the docs: It does not specify much about the setup, so I assume it can handle I'm not so sure that that n:m constellation is often the case in doocs server contexts. For 1:1 contexts we could also add a lock free version (or not, I do not really care): // A producer consumer design pattern
// fifo aehm queue
//
// - Lock free
// - Thread save (for 1 writer / 1 reader situations)
// - Can fail if queue is empty (on pop) or full (on push) (see *)
//
// Design idea taken from vanDooren's MSMVPs blog 5. Jan 2007
// Mixed in with Dr.Dobbs 'Writing Lock-Free Code' 29. Sep 2008
//
// It needs one element more storage than having usable elements
//
// *) This is of no concern because in this server the producer
// will add single entries much more often that the consumer checks
// for entries. But then the consumer will consume all available
// entries at once.
//
// July 2017, Fini Jastrow
#pragma once
#include <atomic>
template<typename ELEMENT_T, std::size_t SIZE>
struct LockFreeQueue {https://mcs-gitlab.desy.de/controls-and-operation/utility/correlator/-/blob/main/LockFreeQueue.hh I found that I have virtually zero use cases where n or m is not 1. |
|
Note to self: Of course not MIT, but nevertheless: https://github.com/cameron314/concurrentqueue |
Explaining the design and use case a bit better in the explanation is a very good idea. To my understanding the queue is capable of handling N producers and M consumers, but it would not scale well if either N or M are big. I'll add a commit to improve the docs. Regarding a |
This adds some explanation of the design (mutexes and condition variables) and under which conditions the queue is expected to perform well. We also document that the message type must be default-constructible and movable in general, and copy-constructible if back() is used.
[why] The queue expects its message type to be default-constructible (see SlidingBuffer), move-constructible and move-assignable. back() expects the type to by copy-constructible.
[why]
Sometimes there are use cases where messages should be directly assigned
to an existing object instead of returning by value. Overloads allowing
this also offer slightly better exception guarantees than the
return-by-value versions in the case of a throwing move operation.
[how]
Add new overloads
bool try_pop(MessageType& msg)
pop(MessageType& msg)
and improve the exception safety documentation of pop() and try_pop() to
be as accurate as possible about what users can expect if their type
throws upon move.
|
Hi everyone, |

This is a port of the
LockedQueuefrom Taskolib. This has already spread via copy&paste to several projects, so it might be good to standardize it. I looked at it and already found some things to improve, especially regarding exception safety and the notification scheme. We might also think about extending it with more features like automatic termination, but for me it would already be good as-is.Regarding the license: I got approval from Marcus to re-license this to MIT; all other authors are present here as well.