//定义WIN32和Linux下通用名称 typedef void wthread; //强制退出线程 int kill_thread(wthread *pthread) { #ifdef WIN32 return(TerminateThread(pthread, 1)); #else return(pthread_cancel(*(pthread_t *)pthread)); #endif } //ExitThread是推荐使用的结束一个线程的方法,当调用该函数时,当前线程的栈被释放,然后线程终止 void exit_thread(void) { #ifdef WIN32 ExitThread(0); #else pthread_exit(0); #endif } //安全关闭THREAD,推荐使用 //如果还有其他的进程在使用这个内核对象,那么它就不会被释放 void free_thread(wthread *pthread) { #ifdef WIN32 CloseHandle(pthread); #else delete (pthread_t *)pthread; #endif } //等待THREAD void wait_thread(wthread *pthread) { #ifdef WIN32 WaitForSingleObject(pthread, INFINITE); #else pthread_join(*(pthread_t *)pthread, NULL); #endif } //创建线程 wthread *create_thread(void *(thread_fn)(void *), void *thread_arg) { int rv; #ifdef WIN32 int id; rv =_beginthreadex( NULL, 0, (unsigned int (__stdcall *)(void *))thread_fn, thread_arg, 0, (unsigned int *)&id); return((wthread *)rv); #else pthread_t *hThread; hThread = new pthread_t; if(hThread == NULL) { return((wthread *)NULL); } rv = pthread_create(hThread, NULL, (void *(*)(void *))thread_fn, thread_arg); if(rv != 0) { /* thread creation failure */ return((wthread *)NULL); } return((wthread *)hThread); #endif } //获得当前THREAD 的ID long get_threadid(void) { #ifdef WIN32 return GetCurrentThreadId(); #else #ifdef LINUX unsigned long thr_id = pthread_self(); thr_id = thr_id << 1; thr_id = thr_id >> 1; return thr_id; #else return pthread_self(); #endif #endif }