简体   繁体   中英

c++ exceptions and pthread_cancel confusion

My code has a pthread_cancel cancelling a thread that has a try block and catch all block (catch (...)).

Due to timing issue when the thread is in try block and the thread gets cancelled, my program gets aborted as the exception raised by pthread NTPL will not be caught by catch all block, due to which the thread cannot cancel.

I looked over for few solutions and I see that if the exception is rethrown by catching it, then the thread cancels normally.

My question is - even if we catch and rethrow, still the exception is not handled finally, then why does not the program abort?

If you want to use pthread_cancel to cancel a thread, the thread has to properly manage its cancellation state, enabling asynchronous cancellation only when that is safe and, in some cases, checking whether it's been cancelled periodically.

If pthread_cancel doesn't do exactly what you need, don't use it. Instead, implement your own cancellation mechanism that has precisely the semantics you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM