扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:51cto 2007年10月14日
关键字:
在本页阅读全文(共3页)
$ ./shm_client shm_client1.cpp:16 Message from pid(4332) : $ ipcs ------ Shared Memory Segments -------- key shmidownerpermsbytesnattch status 0x00001234 327686 sachin 666 136 0 $ ./shm_client shm_client1.cpp:16 Message from pid(4333) : $ ipcrm -m 327686 |
class IObjectWithEvents { public:virtual bool EnqueueEvent(const char * msg) = 0;virtual bool PollForEvents() = 0; }; |
class ObjectWithEvents : public IObjectWithEvents { public:virtual bool EnqueueEvent(const char * msg);virtual bool PollForEvents(); //The event cacheenum { MAX_EVENTS = 16, MAX_EVENT_MSG = 256, };long m_nEvents;pid_t m_alPIDEvents[MAX_EVENTS];char m_aaMsgs[MAX_EVENTS][MAX_EVENT_MSG]; }; |
bool ObjectWithEvents::EnqueueEvent(const char * msg) {if (NULL == msg){return false;} if (MAX_EVENTS == m_nEvents){//IEventSink collection fullreturn false;} int bRetVal = Initializer::LockMutex(); if (0 != bRetVal){return false;} m_alPIDEvents[m_nEvents] = getpid();strncpy(m_aaMsgs[m_nEvents++], msg, MAX_EVENT_MSG - 1); if ((0 == bRetVal) && (0 != Initializer::UnlockMutex())){// Deal with error.} return true; } bool ObjectWithEvents::PollForEvents() {if (0 == m_nEvents){return true;} int bRetVal = Initializer::LockMutex(); if (0 != bRetVal){return false;} pid_t pid = getpid(); for (long i = 0; i < m_npEI; i++){// Does the handler belongs to current process ? if (pid != m_alPID[i]){continue;} //Recheck for NULL if (0 == m_apEI[i]){continue;} for (long j = 0; j < m_nEvents; j++){m_apEI[i]->OnEvent(m_alPIDEvents[j], m_aaMsgs[j]);}} if ((0 == bRetVal) && (0 != Initializer::UnlockMutex())){// Deal with error.} return true; } |
$ ./shm_client1 $ ./ipcs ------ Shared Memory Segments -------- key shmidownerpermsbytesnattch status 0x00001234 360454 sachin 666 4300 0 $ ./shm_client2 shm_client2.cpp:16 Message from pid(4454) : Message from shm_client1 shm_client2.cpp:16 Message from pid(4456) : Message from shm_client2 |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者