Boost::Interprocess mutex with condition variable
I'm trying to get a working implementation of a boost::interprocess named_mutex with a named_condition_variable. I have created the following toy console applications to try and understand what's necessary. The console application 'Sleeper' is run first; it simply creates some resources and then goes to sleep. Then 'Waker' is run with the intention that it would cause the 'Sleeper' instance to resume execution and print out a further message. Ultimately the intention is to have one Waker process and several Sleeper processes. When each application is run in a separate terminal window then the Sleeper process sleeps and the Waker process tries to wake; but it is ineffective. Can anyone advise what is wrong here and how to get the notify_all() to take effect? Thanks Riskybiz. // Sleeper.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/sync/named_mutex.hpp> #include <boost/interprocess/sync/named_condition.hpp> #include <iostream> int _tmain(int argc, _TCHAR* argv[]) { using namespace boost::interprocess; //Clean up resources from any prior instances named_condition::remove("msm_named_condition"); named_mutex::remove("msm_named_mutex"); //shared_memory_object::remove("msm"); //Setup shared memory resources managed_shared_memory *msm = new managed_shared_memory(create_only, "msm", 10000); named_mutex *nm = new named_mutex(create_only, "msm_named_mutex"); named_condition *nc = new named_condition(create_only, "msm_named_condition"); try { scoped_lock<named_mutex> lock(*nm);//acquire scoped lock for mutex std::cout << "Going to sleep now...." << std::endl; nc->wait(lock);//induce wait std::cout << "Wide awake and back in the room" << std::endl;//print this out on waking up } catch(interprocess_exception &ipe) { std::cout << "Error in Sleeper process: interprocess_exception: "<< ipe.what() << std::endl; } //Clean up delete msm; delete nm; delete nc; return 0; } // Waker.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/sync/named_mutex.hpp> #include <boost/interprocess/sync/named_condition.hpp> #include <iostream> int _tmain(int argc, _TCHAR* argv[]) { using namespace boost::interprocess; //Setup shared memory resources managed_shared_memory *msm = new managed_shared_memory(open_only, "msm"); named_mutex *nm = new named_mutex(open_only, "msm_named_mutex"); named_condition *nc = new named_condition(open_only, "msm_named_condition"); try { scoped_lock<named_mutex> lock(*nm);//acquire scoped lock for mutex std::cout << "Going to Notify...." << std::endl; nc->notify_all();//notify to release the wait condition std::cout << "Wakey Wakey....Notified...." << std::endl; } catch(interprocess_exception &ipe) { std::cout << "Error in Waker process: interprocess_exception: "<< ipe.what() << std::endl; } //Clean up delete msm; delete nm; delete nc; return 0; }
One issue with your solution could be having a spurious wake up (http://vladimir_prus.blogspot.ca/2005/07/spurious-wakeups.html) after doing nc->wait(lock);. Wrapping this wait in a while loop will prevent that from happening. Another problem I can see is that your second process cleans up everything before your first process wakes up as nc->notify_all(); is not a blocking call. So the two problems I can see is that your first process could continue after the wait as its not in a loop due to spurious wake ups and if it does wait your second process will delete everything after the notify_all() call as it is running under a scoped mutex and so only after exiting from scope in process 2 will process 1 be able to claim the mutex but by now everything is deleted. -- View this message in context: http://boost.2283326.n4.nabble.com/Boost-Interprocess-mutex-with-condition-v... Sent from the Boost - Users mailing list archive at Nabble.com.
participants (2)
-
Riskybiz
-
rudasi