c++ - std::queue::empty() not working? -
i'm going crazy piece of code. have thread calls regularly method:
void delivermsgq() { if(delmsgq_mutex.try_lock() == false){ return; } while(delmsgq.empty() == false){ std::vector<unsigned char> v = delmsgq.front(); delmsgq.pop(); } delmsgq_mutex.unlock(); } void processinmsgq() { if(inmsgq_mutex.try_lock()){ if(delmsgq_mutex.try_lock() == false){ inmsgq_mutex.unlock(); } }else{ return; } while(!inmsgq.empty()){ std::vector<unsigned char> msg; inmsgq.front()->getdata(msg); std::cout << "adding del-msg-q: " << msg.size() << std::endl; delmsgq.push(msg); delete inmsgq.front(); inmsgq.pop(); } inmsgq_mutex.unlock(); delmsgq_mutex.unlock(); }
i have thread pushing vector queue periodically. these 2 threads ones touch queue delmsgq
.
my problems comes in first function posted, reason delmsgq.empty()
@ point returns false though has no vectors in it, , therefore end calling pop twice. causes size function become huge unrealistic number , program goes segmentation fault. can fix if add additional check right before calling pop, expect checking once enough since i'm using mutexes. other alternative maybe i'm using mutexes wrong, far know proper way use them in case. hoping maybe smarter let me know if there i'm missing? hope code enough, can provide more code if necessary although no other function touch queue failing.
best regards
Comments
Post a Comment