Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to implement C++11 mutex concept for use by std::condition_variable?

I find that the std::mutex implementation in Visual Studio 2013 is too slow. It uses a heavy weight mutex to assure that synchronization can be achieved even between processes which is all fine and dandy; Unless you're not talking to other processes and could really use that extra speed that CRITICAL_SECTION with it's spin-lock offers on Win32.

I tried to implement a fast_recursive_mutex that adheres to the C++11 mutex concept and that fulfills all obligations according to spec. In all senses, it's a drop-in replacement for std::mutex as long as you're not synchronizing between processes.

It works great with std::lock_guard and std::unique_lock. However I encounter problems when trying to use it with std::condition_variable because std::condition_variable::wait(std::unique_lock<std::mutex>&) doesn't admit my fast_recursive_mutex due to the hard coded use of std::mutex.

So my questions are:

  1. Why does wait() not admit another mutex type than std::mutex?
  2. Is there something I can do about it? (Short of re-implementing condition_variable).
like image 919
Emily L. Avatar asked Sep 02 '14 10:09

Emily L.


1 Answers

You can use std::condition_variable_any for any lockable type.

like image 168
Piotr Skotnicki Avatar answered Sep 27 '22 19:09

Piotr Skotnicki