Go: A thread class across the Windows Linux platform

Source: Internet
Author: User

Source: http://blog.csdn.net/dengxu11/article/details/7232681

Following the implementation of a Cthread package class under Windows, here I implement a thread class across the Windows Linux platform

Header file DXThread.h

#ifndef __dxthread_h__#define__dxthread_h__#defineDx_windows//open it on Windows//#define Dx_linux//open it on Linux.#ifdef dx_windows#include<windows.h>#defineDx_callback WINAPItypedef HANDLE Dxhandle;#endif#ifdef dx_linux#include<pthread.h>#defineDx_callbacktypedef pthread_t Dxhandle;#endifclasscdxthread{ Public: Cdxthread (); Virtual~Cdxthread (); Virtual intRun (); BOOLStart (); BOOLJoin (); BOOLDetach (); Private:    Static void* Dx_callback Runthread (void*pparam);    Dxhandle M_hthread; void*M_pparam; unsignedLongM_nret;protected:    BOOLM_bstart; intm_nstate;};#endif //DXThread.h

Source file DXThread.cpp

#include"DXThread.h"Cdxthread::cdxthread () {M_hthread=NULL; M_pparam=NULL; M_nret=0; M_bstart=false; M_nstate=0;} Cdxthread::~Cdxthread () {if(M_bstart && (m_nstate = =0) ) {Join (); }}  intCdxthread::run () {return 0; }  BOOLCdxthread::start () {if(true==M_bstart)return true; BOOLBOK =true; M_bstart=true; #ifdef dx_windowsif(NULL = =(M_hthread= CreateThread (NULL,0, (unsignedLong(dx_callback*) (void*)) &runthread, This,0, NULL)) ) {BOK=false; }#endif#ifdef Dx_linuxif(0! = Pthread_create (&m_hthread, NULL, Runthread, This) ) {BOK=false; }#endif    if(!BOK) {M_bstart=false; return false; }        return true; }  BOOLCdxthread::join () {if(!M_bstart)return false; if(M_nstate = =1)        return false; BOOLret; M_bstart=false; #ifdef dx_windows while(1)    {        if(FALSE = = GetExitCodeThread (M_hthread, &M_nret)) {ret=false;  Break; }        Else          {            if(M_nret = =still_active) {Sleep ( -); Continue;            } closehandle (M_hthread); RET=true;  Break; }    }#endif#ifdef dx_linux ret= (0= = Pthread_join (M_hthread, (void* *) &M_nret));#endifM_hthread=NULL; returnret;} BOOLcdxthread::D Etach () {if(!M_bstart)return false; if(M_nstate = =1)        return false; M_nstate=1; #ifdef dx_windowsreturn(TRUE = =CloseHandle (M_hthread));#endif#ifdef Dx_linuxreturn(0==Pthread_detach (M_hthread));#endif}void* Dx_callback Cdxthread::runthread (void*pparam) {Cdxthread* PThis = (cdxthread*) Pparam; intNret = pthis->Run (); return(void*) nret;}

Invoke instance

#include <stdio.h>#include"DXThread.h"classCTest: Publiccdxthread{ Public:        intRun () {printf ("1..\n"); return 0; }};intMainintargcChar*argv[])    {CTest A;        A.start (); //A.detach ();//Detach Thread        if(false==A.join ()) {printf ("Join failed!\n"); }        return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.