c++ - boost failing to obtain lock -
i working on real-time music application. working boost library newbie. implemented producer/consumer relationship protected syncronizedqueue, implemented 18.11 , 18.12 sections of https://www.quantnet.com/cplusplus-multithreading-boost/. when parsing midi input following exception:
terminate called after throwing instance of 'boost::exception_detail::clone_impl <boost::exception_detail::error_info_injector<boost::lock_error> >' what(): boost::lock_error code parsing input in producer:
rtmidiin *midiin = new rtmidiin(); std::vector<unsigned char> message; int nbytes, i; double stamp; m_queue=queue; // check available ports. unsigned int nports = midiin->getportcount(); if ( nports == 0 ) { std::cout << "no ports available!\n"; goto cleanup; } midiin->openport( 1 ); // don't ignore sysex, timing, or active sensing messages. midiin->ignoretypes( false, false, false ); // install interrupt handler function. done = false; (void) signal(sigint, finish); // periodically check input queue. std::cout << "reading midi port ... quit ctrl-c.\n"; while ( !done ) { stamp = midiin->getmessage( &message ); nbytes = message.size(); if(nbytes>0){ if((message.size()!=1)&&((int)message.at(0)!=137)){ ( i=0; i<nbytes; i++ ) std::cout << "byte " << << " = " << (int)message[i] << ", "; if ( nbytes > 0 ) std::cout << "stamp = " << stamp << std::endl; m_queue->enqueue("asd"); } } } it breaks on first time encounters:
m_queue->enqueue("asd"); when trying execute:
boost::unique_lock<boost::mutex> lock(m_mutex); any appreciated!
edit1:
this synchronizedqueue object. exception thrown when calling enqueue().
template <typename t> class synchronizedqueue { private: std::queue<t> m_queue; // use stl queue store data mutable boost::mutex m_mutex; // mutex synchronise on boost::condition_variable m_cond; // condition wait public: // add data queue , notify others void enqueue(const t& data) { // acquire lock on queue boost::unique_lock<boost::mutex> lock(m_mutex); // add data queue m_queue.push(data); // notify others data ready m_cond.notify_one(); } // lock automatically released here // data queue. wait data if not available t dequeue() { // acquire lock on queue boost::unique_lock<boost::mutex> lock(m_mutex); //lock(); // when there no data, wait till fills it. // lock automatically released in wait , obtained // again after wait while (m_queue.size()==0) m_cond.wait(lock); // retrieve data queue t result=m_queue.front(); m_queue.pop(); return result; } // lock automatically released here };
most times, when stumble on such issue, it's mutex got locked destroyed or wasn't constructed already. sure, queue, passing parser, constructed, when parser threads starts parsing? or might be, exit of loop containing queue local variable stopped? maybe can quick test , add d'tor queue , set breakpoint there.
regards torsten
Comments
Post a Comment