Boost :: threads - how to make a graceful shutdown?

I am trying to improve portability of a C ++ application using boost: threads instead of our own shell on top of Win32 threads, and the problem of gracefully terminating a thread (again) returns its ugly head.

In pure win32, I “interrupt” threads using QueueUserAPC to throw a “thread_interrupt” exception that causes all RAII objects to be cleared as described here . Any "warning" function of the OS can be interrupted this way, so things like mutexes, expectations, sleep mode, serial and socket I / O are all possible breakpoints.

However boost: mutexes, etc. are not “warned” by QueueUserAPC on win32 - they call things like Sleep (n), not SleepEx (n, true))

Threads with a thread have an “interrupt” mechanism (which similarly includes an exception), but it seems to have a drawback that ONLY raises :: thread calls are interrupted, so a third-party socket library (for example) cannot be interrupted.

What to do? I could change the source of the promotion locally to make it interruptible, but this seems like a bad choice, and I don't think it helps portability. Redesigning the entire app to remove the requirement for an elegant stream outage is also an unattractive route ...

+4
source share
1 answer

I have an idea for a partial solution for Win32, but I have not tested it yet:

My thread interrupt method can call both boost::thread.interrupt() , AND QueueUserAPC , and the function called QueueUserAPC just calls boost::interruption_point() to allow forced interruption to take control.

This should mean that the stream is interrupted (but "differently") both when waiting for the synchronization synchronization object, and in the "warning" own windows.

+1
source

Source: https://habr.com/ru/post/1313031/


All Articles