Discussion:
Interrupting Threads with boost::thread::interrupt()
Matthias Vallentin
2008-06-17 09:13:39 UTC
Permalink
Hi folks,

I am having issues with boost::thread::interrupt(). As far as I
understood, calling this function will throw an exception in the
corresponding thread. How can deal with blocking functions, such as a
calls to accept() or when a thread is waiting on a condition variable
to change? Basically the thread blocks somewhere in a while (true) loop.

Injecting interruption points manually with
boost::this_thread::interruption_point() does not seem to help here
either. boost::thread::interrupt() (or interrupt_all in a thread_group),
simply does not return.

Regards,

Matthias
--
Matthias Vallentin
***@icsi.berkeley.edu
pgp/gpg: 0x37F34C16
dhruva
2008-06-17 12:39:29 UTC
Permalink
Hi,

On Tue, Jun 17, 2008 at 2:43 PM, Matthias Vallentin
Post by Matthias Vallentin
corresponding thread. How can deal with blocking functions, such as a
calls to accept() or when a thread is waiting on a condition variable
FWIK, there is no way to get out of blocking calls elegantly. The way
to go about it is to use non-blocking calls. Wait for multiple objects
(on windows I have used this) where one of the object is an event. On
an interrupt, you set the event and the wait will return.
For 'select', SAMBA has a neat trick. They create a pipe, the select
call also listens on one end of this pipe. On a signal, the signal
handler writes a char to the pipe which makes the call to select
return.
For non-blocking accept, you could check the SAMBA implementation of
socketpair. In a single thread, they create a socket, bind, accept,
connect and return the pair of connected sockets.

-dhruva
--
Contents reflect my personal views only!
Anthony Williams
2008-06-17 12:58:00 UTC
Permalink
Post by Matthias Vallentin
I am having issues with boost::thread::interrupt(). As far as I
understood, calling this function will throw an exception in the
corresponding thread. How can deal with blocking functions, such as a
calls to accept() or when a thread is waiting on a condition variable
to change? Basically the thread blocks somewhere in a while (true) loop.
Injecting interruption points manually with
boost::this_thread::interruption_point() does not seem to help here
either. boost::thread::interrupt() (or interrupt_all in a thread_group),
simply does not return.
boost::thread::interrupt() calls the interrupted thread to throw an
exception when it next reaches an interruption point with interruption
enabled. Interruption points are boost::this_thread::sleep,
boost::this_thread::interruption_point, boost::thread::join,
boost::condition_variable::wait and timed_ variants.

See
http://www.justsoftwaresolutions.co.uk/threading/thread-interruption-in-boost-thread-library.html

boost::thread::interrupt() should return almost immediately: it does
not wait for the thread to be interrupted.

Anthony
--
Anthony Williams | Just Software Solutions Ltd
Custom Software Development | http://www.justsoftwaresolutions.co.uk
Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL
Reynolds, John
2008-06-17 14:49:58 UTC
Permalink
Matthias

I believe that only certain boost thread methods such as sleep can be interrupted. What you could do is a loop that calls select with a timeout and only call a blocking accept when there is socket activity or you could loop with a non-blocking accept.

I assume that asio does this much better than what I have described.

John Reynolds
Aircraft Management Technologies Limited
Registered in Ireland, No.337561
Registered office: 1 The Green, Malahide, Co.Dublin, Ireland
Tel: + 353 1 8061000
Fax:+ 353 1 8061025

***@flightman.com
www.flightman.com

THE INFORMATION IN THIS E-MAIL IS CONFIDENTIAL AND MAY BE LEGALLY PRIVILEGED. IT IS INTENDED SOLELY FOR THE ADDRESSEE. ACCESS TO THIS E-MAIL BY ANYONE ELSE IS UNAUTHORISED. IF YOU ARE NOT THE INTENDED RECIPIENT, ANY DISCLOSURE, COPYRIGHT, DISTRIBUTION OR ANY ACTION TAKEN OR OMITTED TO BE TAKEN IN RELIANCE ON IT, IS PROHIBITED AND MAY BE UNLAWFUL.




-----Original Message-----
From: boost-users-***@lists.boost.org [mailto:boost-users-***@lists.boost.org] On Behalf Of Matthias Vallentin
Sent: 17 June 2008 10:14
To: boost-***@lists.boost.org
Subject: [Boost-users] Interrupting Threads with boost::thread::interrupt()

Hi folks,

I am having issues with boost::thread::interrupt(). As far as I understood, calling this function will throw an exception in the corresponding thread. How can deal with blocking functions, such as a calls to accept() or when a thread is waiting on a condition variable to change? Basically the thread blocks somewhere in a while (true) loop.

Injecting interruption points manually with
boost::this_thread::interruption_point() does not seem to help here either. boost::thread::interrupt() (or interrupt_all in a thread_group), simply does not return.

Regards,

Matthias
--
Matthias Vallentin
***@icsi.berkeley.edu
pgp/gpg: 0x37F34C16
Matthias Vallentin
2008-06-18 06:55:08 UTC
Permalink
Post by Reynolds, John
I believe that only certain boost thread methods such as sleep can be
interrupted.
That summarizes pretty much my misunderstanding. In essence I did not
realize that only the methods outlined by Anthony are interruptable via
interrupt().
Post by Reynolds, John
What you could do is a loop that calls select with a
timeout and only call a blocking accept when there is socket activity
or you could loop with a non-blocking accept.
I assume that asio does this much better than what I have described.
Migrating to the non-blocking versions will probably do the trick. It
seems to be a reasonable thing to leverage boost::asio for this purpose.

Thanks for your help,

Matthias

(dhruva: I will probably resort to boost::asio although the Samba tricks
are indeed quite elegant.)
--
Matthias Vallentin
***@icsi.berkeley.edu
pgp/gpg: 0x37F34C16
Loading...