I have a developed a simple polling thread (using Boost 1.39.0) which checks whether a data resource has been accessed within a given timeframe and clears the connection if not. The relevant code can be reviewed below.
My concerns are twofold:
1) Is using interrupt on a sleep appropriate to close down the thread safely? Will the interrrupt wait for the sleep to finish or will it interupt immediately? You can see I catch a thread_interrupted exception just to escape the while loop.
2) Is using a thread which spends most of its time asleep wasteful? Is there a better pattern to implement a simple polling mechanism in standard C++?
boost::xtime xt;
while (1) {
try {
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec += _sleep_secs;
boost::thread::sleep(xt);
//
// logic to check resource access
// etc.
}
catch(boost::thread_interrupted const&) {
return;
}
}