Interrupt a thread that is executing a MKL or other third party function

94 Views Asked by At

¿How can I interrupt a thread that is performing a MKL subroutine? And in other 3rd party libraries? My threads are created with boost::thread, and normally I use interruptions to stop my threads (http://www.boost.org/doc/libs/1_41_0/doc/html/thread/thread_management.html#thread.thread_management.interruption), but how can I do to stop a thread executing MKL code? Any ideas?

Thanks in advance

1

There are 1 best solutions below

0
On

In a nutshell: You cannot.

Thread interruption points as used by Boost have to be used cooperatively, that is the code that is to be interrupted needs to voluntarily trigger a function call that can act as an interruption point. Since MKL does not do that, you are out of luck.

The best way to solve this is to divide your problem into small chunks that don't require long to execute and then check for interruption requests between processing of chunks.

For the sake of completeness: Some APIs allow brute-force cancellation of threads that does not require the canceled thread's permission (Boost is not one of them). However, this is quite dangerous to use as the canceled thread is usually unable to cleanup properly and may thus easily leave the program in a corrupted state. Should you decide to go down that road it is probably a better idea to use different processes instead of threads, as forcefully killing a process is much less error-prone than killing threads.