#include #include #include #include #include #include #include #include #ifndef bool #define bool int #define true 1 #define false 0 #endif #define SLEEP(O) {struct timeval tv;\ tv.tv_usec = (O % 1000)*1000;\ tv.tv_sec = O / 1000;\ select(0, NULL, NULL, NULL, &tv);} pthread_mutex_t m_mMutex; bool m_bThreadRunning; bool do_an_exception; void* ThreadMain(void *Value) { try { while(1) { bool do_an_exception_here = false; pthread_mutex_lock(&m_mMutex); do_an_exception_here = do_an_exception; pthread_mutex_unlock(&m_mMutex); if(do_an_exception_here) throw 1; } } catch(...) { cout << "exception caught by thread" << endl; } pthread_mutex_lock(&m_mMutex); m_bThreadRunning = false; pthread_mutex_unlock(&m_mMutex); return (void *)0; } int main(int argc, char* argv[]) { pthread_mutex_init(&m_mMutex, NULL); pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); pthread_t thid; m_bThreadRunning = true; do_an_exception = false; pthread_create(&thid, &attr, ThreadMain, NULL); SLEEP(1000); pthread_mutex_lock(&m_mMutex); do_an_exception = true; pthread_mutex_unlock(&m_mMutex); while(1) // wait for thread to terminate { try { bool bThreadRunning; pthread_mutex_lock(&m_mMutex); bThreadRunning = m_bThreadRunning; pthread_mutex_unlock(&m_mMutex); if(!bThreadRunning) break; } catch(...) { cout << "exception caught by main" << endl; } } pthread_mutex_unlock(&m_mMutex); pthread_mutex_destroy(&m_mMutex); }