FirteX-高性能全文索引和检索平台API Documentation |
00001 // 00002 // Copyright(C) 2005--2006 Institute of Computing Tech, Chinese Academy of Sciences. 00003 // All rights reserved. 00004 // This file is part of FirteX (www.firtex.org) 00005 // 00006 // Use of the FirteX is subject to the terms of the software license set forth in 00007 // the LICENSE file included with this software, and also available at 00008 // http://www.firtex.org/license.html 00009 // 00010 // Author : 郭瑞杰(GuoRuijie) 00011 // Email : ruijieguo@software.ict.ac.cn,ruijieguo@gmail.com 00012 // Created : 2005/11/18 00013 // 00014 #ifndef _THREAD_WIN32_H 00015 #define _THREAD_WIN32_H 00016 00017 #include <windows.h> 00018 #include <assert.h> 00019 00020 #include "Runnalble.h" 00021 00022 namespace firtex 00023 { 00024 namespace thread 00025 { 00026 class CThread 00027 { 00028 public: 00029 CThread() : m_tid(0),m_hThread(NULL) 00030 { 00031 } 00032 ~CThread() 00033 { 00034 } 00035 public: 00036 00037 bool spawn(CRunnalble* task) 00038 { 00039 // Start the thread. 00040 #if defined(HAVE_BEGINTHREADEX) 00041 m_hThread = (HANDLE)::_beginthreadex(0, 0, &_dispatch, task, 0, (unsigned int*)&m_tid); 00042 #else 00043 m_hThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&_dispatch, task, 0, (DWORD*)&m_tid); 00044 #endif 00045 00046 return m_hThread != NULL; 00047 } 00048 void wait() 00049 { 00050 if(m_hThread == NULL) 00051 return; 00052 ::WaitForSingleObject(m_hThread,INFINITE); 00053 } 00054 00055 static void sleep(unsigned long timeout) 00056 { 00057 Sleep(timeout); 00058 } 00059 00060 00061 protected: 00062 static unsigned int __stdcall _dispatch(void*arg) 00063 { 00064 CRunnalble* task = reinterpret_cast<CRunnalble*>(arg); 00065 assert(task); 00066 00067 task->run(); 00068 00069 // Exit the thread 00070 #if defined(HAVE_BEGINTHREADEX) 00071 ::_endthreadex(0); 00072 #else 00073 ExitThread(0); 00074 #endif 00075 return 0; 00076 } 00077 00078 protected: 00079 DWORD m_tid; 00080 HANDLE m_hThread; 00081 00082 private: 00083 }; 00084 } 00085 } 00086 00087 #endif
http://www.firtex.org http://www.sourceforge.net/projects/firtex