Any member function as a package of thread parameters

xiaoxiao2021-03-06  105

// threadhelper.h: interface for the threadhelper class.////

#if! defined (AFX_THREADHELPER_H__4A9AE80A_FE7A_4DB3_9D6F_3D1A0C80E314__included _) # define afx_threadhelper_h__4a9ae80a_fe7a_4db3_9d6f_3d1a0c80e314__included_

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

#include

// - Any class member function as a package of thread parameters Class Threadif; // - Package Interface Class / In the final implementation of this article (]

class ThreadClass {}; template struct ThreadInfo {typedef DWORD (T :: * TThreadProc) (void *); TThreadProc ThreadProc; T * pClass; void * pData;}; typedef ThreadInfo :: TThreadProc TThreadProc; class ThreadHelper {public: template void * Start (T * pClass, TThreadProc ThreadProc, void * param = 0, int * ref = NULL) {if (NULL == pClass || NULL == ThreadProc) return NULL; // - Thread count static int count = 1; if (_heinfo) count ; if (ref) * ref = count; ThreadInfo * Obj = new threadinfo ; obj-> threadproc = (ThreadInfo < T> :: TthreadProc) threadproc; obj-> pclass = pclass; obj-> pdata = param

_heinfo = (ThreadInfo *) OBJ;

DWORD DWTHREADID = 0; // - Running Handle Handle = CreateThread (Null, 0, (LPTHREAD_START_ROUTINE) __ threadproc, _heinfo, 0, & dwthreadid; return (void *) handle;}

PUBLIC: threadhelper (): _heinfo (null) {}; virtual ~ threadhelper () {};

Private: static dword WinAPI __threadProc (ThreadInfo * Obj) {dWord Ret = (Obj-> pclass -> * (obj-> threadproc); delete obj; return ret;

PRIVATE: ThreadInfo * _heinfo;

// - Thread interfaceclass ThreadIF {virtual DWORD ThreadProc (void * param) {return 0;}; public: // - successfully returned thread handle / else return 0 inline void * ThreadStart (void * param = 0) {return _heThread .Start (THIS, (TTHREADPROC) THREADPROC, PARAM, & _COUNT;}; private: threadhelper _hethread; int _count; public: inline int count () {return_count;}; // - thread count threadif (): _count 0) {};}; // Class ABC}; // - example // Add Threadif class as a parent class threadif_ex: public threadif //, ABC {// - Write a such function ( Thread content) // - Function prototypes cannot be changed (content free) Virtual dword threadproc (void * param) {:: MessageBox (null, "threadif_ex", "Threadhelper threadif", MB_OK; Return 0;}; // - I can get a thread // threadif_ex a; // handle handle = a.threadstart (); // int count = a.count ();

// - Reference //Http://blog.9cbs.net/RingPhone/Archive/2004/10/18/141074.aspx?pending=true/-- This article is inspired by the content shown by the above connection Interface class from you

#ENDIF /! Defined (AFX_THREADHELPER_H__4A9AE80A_FE7A_4DB3_9D6F_3D1A0C80E314__inCluded_)

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

New Post(0)