Evgeny Lazin
Evgeny Lazin

Reputation: 9413

Is it right to catch boost::interrupted_exception?

In my application, threads can be interrupted with boost::thread::interrupt. I want to reuse interrupted thread, can I just catch boost::interrupted_exception, or it is ideologically incorrect?

Upvotes: 0

Views: 90

Answers (1)

Christian.K
Christian.K

Reputation: 49260

I want to reuse interrupted thread

It looks like you can (emphasis added):

boost::thread_interrupted is just a normal exception, so it can be caught, just like any other exception. This is why the "interrupted" flag is cleared when the exception is thrown — if a thread catches and handles the interruption, it is perfectly acceptable to interrupt it again. This can be used, for example, when a worker thread that is processing a series of independent tasks — if the current task is interrupted, the worker can handle the interruption and discard the task, and move onto the next task, which can then in turn be interrupted. It also allows the thread to catch the exception and terminate itself by other means, such as returning error codes, or translating the exception to pass through module boundaries.

Upvotes: 2

Related Questions