Put the member function as a thread start function

xiaoxiao2021-03-06  167

// ThreadObject.h: interface for the cthreadObject class.////

#if! defined (AFX_THREADOBJECT_H__195BAF3F_6D2E_4494_A5EA_C5F0C66F3618__INCLUDED _) # define AFX_THREADOBJECT_H__195BAF3F_6D2E_4494_A5EA_C5F0C66F3618__INCLUDED_

#if _MSC_VER> 1000 # prgma overce # endif //_MSC_VER> 1000

Typedef unsigned (lpvoid para); typedef unsigned * threadid; #include

class CThreadObject {public: void StartThread (); CThreadObject (); virtual ~ CThreadObject (); static DWORD WINAPI ThreadFunc (LPVOID param); protected: void WaitForExit (); virtual DWORD ThreadMemberFunc (); HANDLE m_hThread; DWORD m_ThreadId;};

#ENDIF /! Defined (AFX_THREADOBJECT_H__195BAF3F_6D2E_4494_A5EA_C5F0C66F3618__inCluded_)

// threadObject.cpp: Implementation of the cthreadObject class.////

#include "stdafx.h" #include "testthread.h" #include "threadObject.h"

#ifdef _debug # undef this_filestatic char this_file [] = __ file __; # Define new debug_new # Endif

//// construction / destruction //

CthreadObject :: cthreadObject () {m_hthread = null; m_threadId = 0;}

CthreadObject :: ~ cthreadObject () {if (m_hthread) WaitForeXit ();

void CThreadObject :: StartThread () {m_hThread = (HANDLE) _beginthreadex (NULL, 0, (THREADFUNC) CThreadObject :: ThreadFunc, (LPVOID) this, 0, (THREADID) & m_ThreadId); if (m_ThreadId) TRACE ( "Thread launched / n ");

DWORD WINAPI CTHREADOBJECT :: ThreadFunc (lpvoid param) {cthreadObject * pthread = (cthreadObject *) param; return pthread-> threadmemberfunc ();

DWord CthreadObject :: threadmemberfunc () {trace ("call member variable / n"); return 0;}

Void CthreadObject :: waitforcexit () {WaitForsingleObject (m_hthread, infinite); closehandle (m_hthread);}

CThreadObject * pthread;

转载请注明原文地址:https://www.9cbs.com/read-128396.html

New Post(0)