For a Multithreading program, if one thread dies how you can know that ?
My idea:
(1) use ps to check LWP but it is manually, not efficient.
(2) set a try-catch in each thread, if it exit non-normally, catch it.
(3) let the dying-thread send a message to std::cout or main thread.
Other better ideas ?
thanks
You could use
pthread_cleanup_push(3)at a very early stage in the thread function. The function given topthread_cleanup_pushcould set some flag which a “watcher” thread can pick up.pthread_cleanup_pushis also honoured bypthread_exitand is not bound to exceptions.Edit: A second way to do this: Use
pthread_key_create(3)with a destructor function and callpthread_setspecific(3)early in the thread function. The destructor function can signal the watching thread it’s imminent death.