views:

1330

answers:

2

The title says it all. In Windows environment, is Boost's scoped mutex using WinAPI's critical sections, or something else?

A: 

Win32's CRITICAL_SECTION can only be used among the threads of a single process. If you need to use something between processes, you need a mutex. Boost says nothing about critical sections so I would assume it is using mutexes.

"scoped" just means it has a wrapper that uses RAII to automatically unlock the mutex at the end of a particular scope.

Jason S
Yes, these I already knew. Hmm, guess I need to look into the actual source later on..
nhaa123
If they call it a "mutex", and do not mention the phrase "critical section" assume with very high probability it is not a critical section.
Jason S
Hah, well thought :)
nhaa123
Boost::interprocess is reputable enough that I think that way about it. Clarity is important. If it were someone's independent library I'm not sure I'd be as confident.
Jason S
However, the docs for Boost::mutex clearly say "These are all thread-level mutexes; interprocess mutexes are not supported".
Kylotan
WTF? OP says nothing about the particular type of mutex, only asked about scoping. There is interprocess_mutex and named_mutex. Try reading the docs yourself. http://www.boost.org/doc/libs/1_35_0/doc/html/interprocess/synchronization_mechanisms.html#interprocess.synchronization_mechanisms.mutexes.mutexes_interprocess_mutexes
Jason S
The original poster asked about a mutex in Boost, so it's natural to assume this refers to Boost::mutex rather than some more specific mutex. This is part of the thread library rather than the interprocess library, and in the past did accordingly map to a critical section on Win32 rather than an OS-level mutex. Thus "I would assume it is using mutexes" is a reasonable assumption but a false one in this case. Even if the OP meant a different kind of mutex, this fact means that the general assumption does not apply universally.
Kylotan
:shrug: I don't see why it's more natural to assume the Boost::thread library than the Boost::interprocess library... but whatever.
Jason S
+11  A: 

The current version of boost::mutex uses neither a Win32 CRITICAL_SECTION, nor a Win32 Mutex. Instead, it uses atomic operations and a Win32 Event for blocking waits.

Older versions (boost 1.34.1 and prior) were a wrapper around CRITICAL_SECTION on Windows.

Incidentally, the mutex itself is not scoped. The boost::mutex::scoped_lock type and, in recent versions, boost::lock_guard<boost::mutex> and boost::unique_lock<boost::mutex> provide RAII wrappers for locking a mutex to ensure you don't forget to unlock it.

The boost::lock_guard<> and boost::unique_lock<> templates work with any type with lock() and unlock() member functions, so you can use them with inter-process mutexes if desired.

Anthony Williams
Thank you for the answer.
nhaa123