Hot answers tagged

5

If you swap pointers to the mutexes after a failed iteration, you can get better performance than either your original code, or the ordered solution shown in the currently accepted answer. void lock(pthread_mutex_t* m1, pthread_mutex_t* m2) { while(1) { pthread_mutex_lock(m1); if(pthread_mutex_trylock(m2) == 0) { // if lock succesfull break; ...


4

There's not much to say, it's not much code, it's documented and from reading it I could not find an error in the locking mechanism. public void performInsideRead(Runnable lambda) { A better name would be performSafeRead or performRead, as "inside" does not necessarily give any additional meaning. lambda should be called action or readAction, as ...


4

I strongly recommend not inheriting from standard types, and not using private inheritance even with your own types. If a recursive_shared_mutex IS-A shared_mutex, then announce it publicly; if it IS-NOT-A shared_mutex, then don't use inheritance at all! (Godbolt. D_view here is isomorphic to std::reference_wrapper<D>, for what that's worth.) std::...


3

Not a review but my version of the same as usage example of Task, BlockingCollection and IDisposable implementation. It's thread-safe, thus you may create it and add jobs from multiple threads. public class Consumer<T> : IDisposable { private readonly int _maxConcurrency; private readonly Action<T> _action; private readonly ...


3

You have entered in the realm of concurrency. So there will be one or more functions which are competing to be executed. In other words there is no guarantee that your code will run in parallel. If there is enough executor (CPU cores) then the runtime might schedule several threads to different cores. If there is not sufficient resource to be utilized then ...


2

There's too much state Each queue has four mutexes, four counters and two condition variables. That is way too much. You could do this with just a single mutex and condition variable. In your push() function, you first have to hold a mutex at least once to check if there are empty slots (if not, you have to wait for a condition variable to be signalled, ...


Only top voted, non community-wiki answers of a minimum length are eligible