📄 warthreadunix.cpp
字号:
#include "StdAfx.h"#include "WarThread.h" // class implemented// Unix/Pthreads spesific part of WarThread#ifndef THREAD_PTHREAD# error "Need ptheards"#endif#ifndef WAR_SIGNAL_H_INCLUDED# define WAR_SIGNAL_H_INCLUDED# include <signal.h>#endif#ifndef WAR_IOSTREAM_IBNCLUDED# define WAR_IOSTREAM_IBNCLUDED# include <iostream>#endifusing namespace std;static void sig_int_handler(int Sig);/////////////////////////////// PUBLIC /////////////////////////////////////////============================= LIFECYCLE ====================================//============================= OPERATORS ====================================//============================= OPERATIONS ===================================void WarThread::SetPriority(const WarPrioritiesDefE Priority)throw(WarException){ ; // Not implemented}//============================= ACESS ===================================//============================= INQUIRY ===================================pthread_t WarThread::GetCurrentThreadId(){ return pthread_self();}/////////////////////////////// PROTECTED ////////////////////////////////////////////////////////////////// PRIVATE ///////////////////////////////////void WarThread::InitializeThread() throw(WarException){ static bool is_initialized; int err = 0; if (!is_initialized) { // Set up signal handlers#ifdef SIGPIPE signal(SIGPIPE, SIG_IGN);#endif#ifdef SIGLOST signal(SIGLOST, SIG_IGN);#endif signal(SIGXCPU, SIG_IGN);#ifdef SIGXFSZ signal(SIGXFSZ, SIG_IGN);#endif#ifdef SIGHUP signal(SIGHUP, SIG_IGN);#endif#ifdef SIGCHLD signal(SIGCHLD, SIG_IGN);#endif#ifdef SIGINT signal(SIGINT, sig_int_handler);#endif#ifdef SIGTERM signal(SIGTERM, sig_int_handler);#endif#ifdef SIGQUIT signal(SIGQUIT, sig_int_handler);#endif } // Set up TLS data if (!is_initialized) { err = ::pthread_key_create(&mTsdKey, NULL); if (err) { WarThrow(WarError(WAR_THREADERR_FAILED_TO_INITIALIZE_TLS,err), NULL); } } if ((err = ::pthread_setspecific(mTsdKey, new WarThreadSpesificData)) != 0) { WarThrow(WarError(WAR_THREADERR_FAILED_TO_INITIALIZE_TLS, err), NULL); }}void WarThread::CreateThread(war_thread_id_t & thread_id) throw(WarException){ int err = ::pthread_create(&thread_id, NULL, war_thread_entry, (void *) this); if (err) WarThrow(WarError(WAR_THREADERR_FAILED_TO_CREATE, err), NULL);}voidWarThread::ExitThread(){ pthread_exit(NULL);}void * WarThread::war_thread_entry(void *lParam){ WarThread *pThread = (WarThread *)lParam;#if WAR_CATCH_ALL try#endif { pThread->StartFunc(); }#if WAR_CATCH_ALL catch(...) { WarLogError("WarThread::war_thread_entry()", "Caught unknown exception from StartFunc()", NULL); }#endif return NULL;}static void sig_int_handler(int Sig){ signal(Sig, SIG_IGN); WarThread *p = NULL;#if WAR_CATCH_ALL try#endif { p = WarThread::GetCurrentThread(); }#if WAR_CATCH_ALL catch(...) { ; // Nothing }#endif#ifdef DEBUG if (cerr.good()) { cerr << "sig_int_handler(" << Sig << ") - Caught signal from thread: "; if (p) cerr << (unsigned)p->GetThreadId() << endl; else cerr << "main thread" << endl; }#endif if (p) { // Worker thread p->SetExitFlag(); } else { // Main thread if (Sig == SIGQUIT) {#ifdef DEBUG if (cerr.good()) cerr << "sig_int_handler(" << Sig << "): Terminating program." << endl;#endif exit(-2); } } signal(Sig, sig_int_handler);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -