c++ - Windows Errorcode : 298 for Bounded buffer solution in vc++ -
- i have encountered bounded buffer problem in project, solving this, im using 2 semaphores full , empty.
- write operation waits empty semaphore signal , signals full semaphore after finishing write.
- read operation waits full semaphore signal , signals empty semaphore after read.
- since im using blocking calls read , write, each read , write happens in sequence.
- i'm implementing in vc++ in windows, im facing windows errorcode:298 while signalling empty semaphore says many posts made semaphore.
- what possible causes error 'too may posts made semaphore' ?
- list item
semaphore creation:
string semname = m_mqname; semname.append(semaphore_full_name_suffix); cout<<"\n <messagequeue<dtype, size>::createmsgqsemaphores ()> semaphore name = "<<semname<<endl; m_mqfullsemaphore = createsemaphore( null, // default security attributes 0, // initial count size, // maximum count semname.c_str()); //semaphore name if (m_mqfullsemaphore == nullptr) { cout<<"error::createsemaphore failed, name:"<<semname<<",error code:"<<getlasterror()<<endl; closehandle(m_mqmutex); // close existing m_mqmutex createsemaphoresstatus = createmsgqsemaphoresfailederror; } else if (error_already_exists == getlasterror()) { cout<<"\n <messagequeue<dtype, size>::createmsgqsemaphores ()>::warning: 'full' semaphore exist.. "<<semname<<endl; } else //cout<<"***info:: semaphore created: m_mqfullsemaphore= "<<m_mqfullsemaphore<<endl; //------------------------------------------empty semaphore creation--------------------------------------------// semname = m_mqname; semname.append(semaphore_empty_name_suffix); //cout<<"\n <messagequeue<dtype, size>::createmsgqsemaphores ()> semaphore name = "<<semname<<endl; m_mqemptysemaphore = createsemaphore( null, // default security attributes size, // initial count size, // maximum count semname.c_str()); // semaphore name if(m_mqemptysemaphore == nullptr) { cout<<"\n <messagequeue<dtype, size>::createmsgqsemaphores ()>::error: create empty semaphore failed.. "<<semname<<endl; closehandle(m_mqmutex); // close existing m_mqmutex createsemaphoresstatus = createmsgqsemaphoresfailederror; }
consumer thread (reader in project)
dword dwfullsemaphorewaitresult = waitforsingleobject(m_mqfullsemaphore, timeoutval);//wair full semaphore if(dwfullsemaphorewaitresult== wait_object_0) // got full semaphore { //proceed further dword dwmutexwaitresult = waitforsingleobject( m_mqmutex, timeoutval); // no time-out interval //queue_mutex_handler mutexhandler(m_mqmutex); //raii log_msg("success: aquire m_mqfullsemaphore:"<<m_mqname); switch (dwmutexwaitresult) { case wait_object_0: // got ownership of mutex { log_msg("success: aquire m_mqmutex:"<<m_mqname); size_t qsize = 0; if(! m_pmsgqueueimpl->dequeue(destmsg,qsize)) { log_msg("success: reached here:"<<m_mqname); log_msg("error: dequeue failed, msg queue empty:"<< m_mqname); //releasemutex(m_mqmutex); execresult = mqstate_queue_empty_error; if(0 == releasemutex(m_mqmutex)) { log_msg("release mutex error:"<<getlasterror()); } } else { int semcount = 0; log_msg("mq pop successful:"<< m_mqname<<", qsize="<<qsize); //releasemutex(m_mqmutex); if(0 == releasemutex(m_mqmutex)) { log_msg("release mutex error:"<<getlasterror()); } if ( 0 == releasesemaphore( m_mqemptysemaphore, // handle semaphore 1, // increase count 1 null)) // not interested in previous count { //log_msg("semcount = "<<semcount); log_msg("release empty semaphore error: "<<getlasterror()); } else { //log_msg("semcount = "<<semcount); log_msg("empty semaphore signalled successfully"); } return (int)qsize; } break; } case wait_timeout: { log_msg("error: failed aquire mutex:"<<m_mqname<<", due timeout:"<<timeoutval); execresult = mqstate_qoptimeout_error; break; } default: // thread got ownership of abandoned mutex { log_msg("error: failed aquire mutex:"<<m_mqname<<", due getlasterror:"<<getlasterror()); execresult = mqstate_queue_unhandled_error; } } // end of switch (dwmutexwaitresult) } else if(dwfullsemaphorewaitresult == wait_timeout) { log_msg("error: failed aquire m_mqfullsemaphore:"<<m_mqname<<", due timeout:"<<timeoutval); execresult = mqstate_qoptimeout_error; } else { log_msg("error: failed aquire m_mqfullsemaphore:"<<m_mqname<<", getlasterror:"<<getlasterror()); execresult = mqstate_queue_unhandled_error; } if(execresult != mqstate_qop_success) return execresult; //================================================================================================= //log_func_end; return execresult;
producer thread
msg_queue_status execresult = mqstate_qop_success; //wait empty semaphore dword dwsemaphorewaitresult = waitforsingleobject( m_mqemptysemaphore, // handle mutex timeoutvalinms); // time-out interval log_msg("m_mqemptysemaphore: "<<m_mqemptysemaphore); log_msg("got m_mqemptysemaphore"); //wait mutex if(dwsemaphorewaitresult == wait_object_0) { dword dwmutexwaitresult = waitforsingleobject( m_mqmutex, // handle mutex timeoutvalinms); // time-out interval //queue_mutex_handler mutexhandler(m_mqmutex); log_msg("got m_mqmutex"); switch(dwmutexwaitresult) { case wait_object_0: log_msg("going send message"); if(m_pmsgqueueimpl->enqueue(srcmsg) ) { log_msg("message sent successfully"); //int semcount; if(0 == releasemutex(m_mqmutex)) { log_msg("release mutex error:"<<getlasterror()); } if ( 0 == releasesemaphore( m_mqfullsemaphore, // handle semaphore 1, // increase count 1 null)) // not interested in previous count { //log_msg("semcount = "<<semcount); log_msg("release full semaphore error: "<<getlasterror()); } else { //log_msg("semcount = "<<semcount); log_msg("full semaphore signalled successfully"); } ///++++++++++++++ } else { log_msg("error: enqueue failed, msg queue full, qname = "<< m_mqname); if(0 == releasemutex(m_mqmutex)) { log_msg("release mutex error:"<<getlasterror()); } execresult = mqstate_queue_full_error; } break; case wait_timeout: log_msg("error: failed aquire msgqueue mutex:"<<m_mqname<<", due timeout:"<<timeoutvalinms); execresult = mqstate_qoptimeout_error; break; default: log_msg("error: failed aquire msgqueue mutex:"<<m_mqname); execresult = mqstate_queue_unhandled_error; }//switch ends } else if(wait_timeout==dwsemaphorewaitresult) { log_msg("error: failed aquire msgqueue semaphore:"<<m_mqname<<", due timeout:"<<timeoutvalinms); execresult = mqstate_qoptimeout_error; } else { log_msg("error: failed aquire msgqueue semaphore:"<<m_mqname); execresult = mqstate_queue_unhandled_error; } //raii //log_func_end; return execresult;
Comments
Post a Comment