c++ - Blocking thread interrupt -
the following process function reads data off queue , processes it. wait_and_pop
function of masterqueue
performs blocking call. therefore, control not move ahead until there exists data on queue can read.
class context { void launch() { boost::thread thread1(boost::bind(&context::push,this ) ); boost::thread thread2(boost::bind(&context::process,this ) ); std::cout<<"joining thread1"<<std::endl; thread1.join(); std::cout<<"joining thread2"<<std::endl; thread2.join(); } void process() { data data; while(status) { _masterqueue.wait_and_pop(data); //blocking call //do data } } void push() { while(status) { //depending on internal logic, data generated _masterqueue.push(data); } } };
status boolean(in global scope). boolean set true default. changed false when signal caught such sigint, sigsesv etc. in such case, while loop exited , program can exited safely.
bool status = true; void signalhandler(int signum) { std::cout<<"signum"<<signum; status = false; exit(signum); } int main() { signal(sigabrt, signalhandler); signal(sigint, signalhandler); signal(sigsegv, signalhandler); context context; context.launch(); }
since, no new data pushed thread2 when signal thrown, control in thread1 stuck @
_masterqueue.wait_and_pop(data);
how force blocking call interrupted?
- is possible implement without changing internal workings of wait_and_pop
- placing timeout not option, since data may arrive on queue once in couple of hours or multiple times second
- do push specific type of data on receiving signal, e.g int_max/int_min, process function coded recognise , exits loop.
timeout answer break loop on getting answer or having interrupt
you spoof things bit having inturrupt push noop queue
Comments
Post a Comment