help with thread::interrupt

Hi All I am playing with boost::thread and it is not clear how thread::interrupt actually works. If I understood clearly the thread will be interrupted the next time it enters one of the predefined *interruption points*http://www.boost.org/doc/libs/1_43_0/doc/html/thread/thread_management.html#...; since thread::join is one interruption point, after I call tr.interrupt() I am expected that the thread will throw a boost::thread_interrupted exception once I call tr.join() right? . Below the sample code I am working now and I cannot get this behavior. Maybe I misunderstood the meaning of interrupt. If I uncomment the call to this_thread::sleep() I can see the thread_interrupted exception. Can you help me in understanding? Regards void f1_good(){ try{ int i = 0; while(true){ i++; // boost::this_thread::sleep( boost::posix_time::milliseconds( 0 ) ); // if I // uncomment this line calling interrupt will throw thread_interrupted } }catch(boost::thread_interrupted& ex ){ std::cout << "BREAK HERE!!" << std::endl; std::cout.flush(); } } int main( int argc, char** argv ){ boost::thread tr( f1 ); boost::this_thread::sleep( boost::posix_time::seconds( 3 ) ); std::cout << "calisl to INTER." << std::endl; std::cout.flush(); tr.interrupt(); std::cout << "PAUSE BEFORE JOIN." << std::endl; std::cout.flush(); boost::this_thread::sleep( boost::posix_time::seconds( 5 ) ); std::cout << "join" << std::endl; tr.join(); }

On Jun 13, 2010, at 11:43 AM, Conoscenza Silente wrote:
Hi All I am playing with boost::thread and it is not clear how thread::interrupt actually works.
If I understood clearly the thread will be interrupted the next time it enters one of the predefined interruption points; since thread::join is one interruption point, after I call tr.interrupt() I am expected that the thread will throw a boost::thread_interrupted exception once I call tr.join() right? .
I believe that if your thread had started a second thread and was waiting on thread::join() for the second level thread to finish, and if you called thread::interrupt on the first thread, then you would see the exception. i.e. void levelTwo (void) { while (true) { ; } } void levelOne (void) { try { boost::thread t2 (levelTwo); t2.join(); } catch (const boost::thread_interrupted& ex ) { std::cout << "levelOne was interrupted" << std::endl; } } int main( int argc, char** argv ){ boost::thread t1( levelOne ); boost::this_thread::sleep( boost::posix_time::seconds( 3 ) ); std::cout << "calisl to INTER." << std::endl; std::cout.flush(); t1.interrupt(); } - Rush

Hi Rush
I thank you very much! It works and now boost::interrupt is more clear to
me.
Can I ask you and who might help me one more question if you have time?
I hope is an easy one.
In a scenario where my thread is very CPU consuming , in your opinion which
is the better
way to create an interruption point?
#1 using a call to boost::this_thread::interruption_point()
#2 or using boost::this_thread::sleep( boost::posix_time::milliseconds( 0 )
)
Initially I thought the probably #1 might be better since seems created just
to throw the boost::thread_interrupted without any extra overhead due to
call to OS time posix libraries.
I gave a thought and I think that it might be better to chose the second one
since giving sleep( 0 ) might help the
OS scheduler in switching to another thread blocked. I am running under
Linux but I think that also in Windows might be the same.
Do you think that this reasoning make sense ?
Beside my specific case is it more efficient to call
boost::this_thread::interruption_point()?
Kind Regards
AFG
On Sun, Jun 13, 2010 at 9:07 PM, Rush Manbert
On Jun 13, 2010, at 11:43 AM, Conoscenza Silente wrote:
Hi All I am playing with boost::thread and it is not clear how thread::interrupt actually works.
If I understood clearly the thread will be interrupted the next time it enters one of the predefined *interruption points*http://www.boost.org/doc/libs/1_43_0/doc/html/thread/thread_management.html#...; since thread::join is one interruption point, after I call tr.interrupt() I am expected that the thread will throw a boost::thread_interrupted exception once I call tr.join() right? .
I believe that if your thread had started a second thread and was waiting on thread::join() for the second level thread to finish, and if you called thread::interrupt on the first thread, then you would see the exception.
i.e.
void levelTwo (void) { while (true) { ; } }
void levelOne (void) { try { boost::thread t2 (levelTwo); t2.join(); } catch (const boost::thread_interrupted& ex ) { std::cout << "levelOne was interrupted" << std::endl; } }
int main( int argc, char** argv ){
boost::thread t1( levelOne );
boost::this_thread::sleep( boost::posix_time::seconds( 3 ) );
std::cout << "calisl to INTER." << std::endl; std::cout.flush(); t1.interrupt(); }
- Rush
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Hi, On Jun 13, 2010, at 2:19 PM, Conoscenza Silente wrote:
Hi Rush I thank you very much! It works and now boost::interrupt is more clear to me.
Can I ask you and who might help me one more question if you have time? I hope is an easy one.
In a scenario where my thread is very CPU consuming , in your opinion which is the better way to create an interruption point?
#1 using a call to boost::this_thread::interruption_point() #2 or using boost::this_thread::sleep( boost::posix_time::milliseconds( 0 ) )
I would use #1 simply because its behavior does not depend on anyone's implementation of sleep(). Somewhere under the hood the Boost sleep call makes a system sleep call that may or may not return immediately when the calling argument is zero. It might return immediately today and next year it might put your thread to sleep for an unexpected 10 msec.
Initially I thought the probably #1 might be better since seems created just to throw the boost::thread_interrupted without any extra overhead due to call to OS time posix libraries.
I gave a thought and I think that it might be better to chose the second one since giving sleep( 0 ) might help the OS scheduler in switching to another thread blocked. I am running under Linux but I think that also in Windows might be the same.
I'm not sure that you calling sleep(0) is really going to help the scheduler. It sounds like this thread has a lot of work to do, so I would just let the system handle the time slicing. That's its job. If you discover later (through measurement) that this thread is a problem you could look at alternative implementations. But safety and reliability of the implementation (my first set of comments above) is, IMHO, the most important thing to worry about. Best regards, Rush
Do you think that this reasoning make sense ? Beside my specific case is it more efficient to call boost::this_thread::interruption_point()?
Kind Regards AFG
On Sun, Jun 13, 2010 at 9:07 PM, Rush Manbert
wrote: On Jun 13, 2010, at 11:43 AM, Conoscenza Silente wrote:
Hi All I am playing with boost::thread and it is not clear how thread::interrupt actually works.
If I understood clearly the thread will be interrupted the next time it enters one of the predefined interruption points; since thread::join is one interruption point, after I call tr.interrupt() I am expected that the thread will throw a boost::thread_interrupted exception once I call tr.join() right? .
I believe that if your thread had started a second thread and was waiting on thread::join() for the second level thread to finish, and if you called thread::interrupt on the first thread, then you would see the exception.
i.e.
void levelTwo (void) { while (true) { ; } }
void levelOne (void) { try { boost::thread t2 (levelTwo); t2.join(); } catch (const boost::thread_interrupted& ex ) { std::cout << "levelOne was interrupted" << std::endl; } }
int main( int argc, char** argv ){
boost::thread t1( levelOne );
boost::this_thread::sleep( boost::posix_time::seconds( 3 ) );
std::cout << "calisl to INTER." << std::endl; std::cout.flush(); t1.interrupt(); }
- Rush
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Conoscenza Silente
-
Rush Manbert