Reputation: 11671
I currently have a boost::mutex code section in my application that looks something like this
{//Lock
boost::unique_lock<boost::mutex> lock(some_mutex);
while(container.empty())
{
condition_var.wait(lock);
}/*Block if empty - for spurious wakeup*/
......
,,,,,,
}//Unlock
Now some_mutex is of type boost::mutex and condition_var is of type boost::condition_variable
Now condition_var is triggered with condition_var.notifyone()
method unfortunately this method takes boost::mutex to function.
I am planning on removing the boost::mutex and using windows provided CRITICAL_SECTION. However I believe the boost condition does not work with windows CRITICAL_SECTION
any suggestion on what my options might be to replace the boost::mutex with CRITICAL_SECTION with minimum change in the above code ?
Upvotes: 0
Views: 409
Reputation: 3135
You can use boost::condition_variable_any
with any mutex-like object such a CRITICAL_SECTION
. (as DaveS comment says)
But, I strongly recommend you to do profiling speed and compare your first simple version to the following code.
#include <windows.h>
#include <boost/thread/condition_variable.hpp>
// wrapper class for CriticalSection
class win32cs_mutex : boost::noncopyable {
public:
win32cs_mutex() { ::InitializeCriticalSection(&cs_); }
~win32cs_mutex() { ::DeleteCriticalSection(&cs_); }
void lock() { ::EnterCriticalSection(&cs_); }
void unlock() { ::LeaveCriticalSection(&cs_): }
private:
CRITICAL_SECTION cs_;
};
win32cs_mutex some_mutex;
boost::condition_variable_any condition_var;
// client code
{
boost::unique_lock<win32cs_mutex> lock(some_mutex);
while(container.empty())
{
condition_var.wait(lock);
}
...
}
Upvotes: 1