Hello All,
Just wrote a simple boost named mutex and condition based IPC test tool. Boost has done life so simple, with these kind of interfaces to basic programming.
-thanks
Harish
<code>
#include <boost/interprocess/sync/named_condition.hpp>
#include <boost/interprocess/sync/named_mutex.hpp>
namespace ipc = boost::interprocess;
int main(int argc, char **argv)
{
std::string lMutexNamed(“harish_mutex”),lCondNamed(“harish_cond”);
std::string lCondNamed2(“harish_cond2″),lMutexNamed2(“harish_mutex2″);
ipc::named_mutex::remove(lMutexNamed.c_str());
ipc::named_mutex appTraceCtrlMutex(boost::interprocess::create_only,lMutexNamed.c_str());
ipc::named_condition::remove(lCondNamed.c_str());
ipc::named_condition appTraceCtrlCondition(boost::interprocess::create_only,lCondNamed.c_str());
ipc::named_condition::remove(lCondNamed2.c_str());
ipc::named_condition appTraceCtrlCondition2(boost::interprocess::create_only,lCondNamed2.c_str());
ipc::named_condition::remove(lMutexNamed2.c_str());
ipc::named_mutex appTraceCtrlMutex2(boost::interprocess::create_only,lMutexNamed2.c_str());
int i=10;
while(i) {
{
ipc::scoped_lock<ipc::named_mutex> lock(appTraceCtrlMutex);
printf(“Acquired lock:[%s]\n”,lMutexNamed.c_str());
//sleep on condition
appTraceCtrlCondition.wait(lock);
printf(“Notification recv from t1\n”);
}
{
ipc::scoped_lock<ipc::named_mutex> lock(appTraceCtrlMutex2);
appTraceCtrlCondition2.notify_one();
printf(“Notification sent to t2\n”);
}
i–;
}
printf(“Done with creation of named mutex/condition \n”);
}
t2.cpp
#include <boost/interprocess/sync/named_condition.hpp>
#include <boost/interprocess/sync/named_mutex.hpp>
namespace ipc = boost::interprocess;
int main(int argc, char **argv)
{
std::string lMutexNamed(“harish_mutex”),lCondNamed(“harish_cond”),lCondNamed2(“harish_cond2″),lMutexNamed2(“harish_mutex2″);
try {
ipc::named_mutex appTraceCtrlMutex(boost::interprocess::open_only,lMutexNamed.c_str());
ipc::named_condition appTraceCtrlCondition(boost::interprocess::open_only,lCondNamed.c_str());
ipc::named_mutex appTraceCtrlMutex2(boost::interprocess::open_only,lMutexNamed2.c_str());
ipc::named_condition appTraceCtrlCondition2(boost::interprocess::open_only,lCondNamed2.c_str());
int i=10;
while(i) {
{
ipc::scoped_lock<ipc::named_mutex> lock(appTraceCtrlMutex);
// wake-up the guy sleeping on condition
appTraceCtrlCondition.notify_one();
printf(“Sent notification to t1\n”);
}
{
ipc::scoped_lock<ipc::named_mutex> lock(appTraceCtrlMutex2);
appTraceCtrlCondition2.wait(lock);
printf(“Notification recv from t1\n”);
}
i–;
}
}
catch(boost::interprocess::interprocess_exception &ex){
printf(“handleTraceClient harish_mutex\n”);
}
printf(“Done with creation of named mutex/condition \n”);
}
</code>