|
Boost Users : |
Subject: Re: [Boost-users] help with thread::interrupt
From: Rush Manbert (rush_at_[hidden])
Date: 2010-06-13 19:23:35
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 <rush_at_[hidden]> 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_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net