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;
}