|
Threads and ExceptionsIt's important to realise that threads and exceptions are not dealt with at all in the ISO/IEC C++ standard. Hence, there's no standard behaviour to be expected. However, there are some restrictions forced upon the library from the low-level libraries.For example, in the LinuxThreads POSIX thread library, the thread creating function is declared as int pthread_create(pthread_t*, pthread_attr_t*, void*(*)(void*), void*) throw (); static void* posix_thread_function(void* t) { THREAD_POSIX_THREAD* thr = (THREAD_POSIX_THREAD*)t; return t->operator()() throw(); } void threadmm::thread_traits<pthread_t,POSIX_THREAD_ATTR>(thread_type& t, attr_type* a, client_type* c) { pthread_create(&t, a->implementation(), posix_thread_function, (void*)c); } throw() in the declaration of pthread_create says that this global function will not throw exceptions. Hence, if the client's operator()() member function to throws an exception out of it self, a runtime error will occure and the library function std::terminate will eventually be called, terminating the application.As the example above illustrates, there's absolutely no way to deliever exceptions from the thread client to the main program. Hence, the best way to deal with exceptions in a thread is to catch them in the thread: class my_thread : public threadmm::thread { public: void* operator()() throw() { try { // code that might throw exceptions. } catch (...) { // Deal with the exception } return 0; } }; Note, that the barrier , condition , mutex , spinlock , and semaphore may throw exceptions in case of deadlocks and the like. It's documented in the class descriptions which exceptions a class my throw.
The POSIX interface has a fall-back Last update Tue Nov 9 12:40:50 2004 Christian Holm Created by DoxyGen 1.3.9.1 |