Boost logo

Boost :

Subject: Re: [boost] thread interruption request
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2016-04-10 19:55:42


On 11/04/2016 02:24, Gaetano Mendola wrote:
> I have a Class accepting functions to be executed
> asynchronously this is implemented in terms of
> boost threads:
>
> T::operator()() {
> while (true) {
> foo();
> boost::this_thread::interruption_point();
> }
> }
>
> One of those functions performs some operations
> between a:
>
> try { /*code*/ } catch(...) { }
>
> this basically makes my Class not interruptible indeed
> that boost::this_thread::interruption_point() is useless
> if a boost::thread_interrupted has been catched inside
> the foo.

The recommended practice is to modify that to not swallow all exceptions:

     try
     {
         /* code */
     }
     catch (const boost::thread_interrupted&)
     {
         throw;
     }
     catch (...)
     {
         /* log or whatever */
     }

or just:

     try { /* code */ } catch(...) { /* cleanup */ throw; }

There are a few such special exceptions that should never be discarded.
  But in general you should only ever use catch(...) blocks as a way to
do try/finally style cleanup (although using RAII types is even better)
and always rethrow at the end.

It's bad practice to blindly discard exceptions, partly for just this
sort of reason but also because it hides bugs and could result in
corrupted program state. Try to only catch the specific exception type
that you want to suppress (and see if the code has an alternate API that
returns the error instead of throwing it).


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk