Emily L.
Emily L.

Reputation: 5931

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

Upvotes: 8

Views: 926

Answers (2)

kozmo
kozmo

Reputation: 51

I believe std::mutex implementation in Visual Studio 2012/2013 already uses critical sections. Just check VSDIR\VC\crt\thr\mutex.c

You can also empirically check this using std::mutex::native_handle() method and cast what's returned to CRITICAL_SECTION.

Upvotes: -1

Piotr Skotnicki
Piotr Skotnicki

Reputation: 48447

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

Upvotes: 6

Related Questions