Wire package tips: the member function pointer of any class as a parameter

xiaoxiao2021-03-06  81

Thread package is very simple, there is not much thing, but if I tell you, I have a member function start in this thread class, the role is to turn on the thread, which can be called:

Class a {public: dword threadfunca; void startthread (); cmythread m_thread;}

DWORD A :: ThreadFunca (long lparam) {// thread execution code ...} Class B {public: DWORD threadFuncb (long lparam); void startthread (); cMythread m_thread;}

DWORD B :: ThreadFuncb (long lparam) {// thread execution code ...}

// Call code: Void a :: startthread () {m_thread.start (this, (threadfunc) threadfunca);} void b :: startthread () {m_thread.start (this, (threadfunc) threadfuncb;} Are you not Will you feel amazing? A and B are two classes that are completely unsatisfactory, but their member functions can be passed as a parameter. This start is not a macro, how is its parameter defined? Smart, you have already thought of template, good, this start is a template function, but when you call the template, you don't see the XXX format of the template call, and the class calling, this thread class does not need to generate an object. Inferring specific data types, you will use a little skill.

First, imagine that this start mechanism is implemented, press thread implementation rules, must first place the thread function to a global or static thread handler, then transfer to the specific member handler. In this way, this function must know the member function pointer of class A or B, plus the parameters that the original thread needs to be incompatible, so the parameters that are incoming this global thread process function must be a structure. Define this structure: Template struct threadinfo {typedef dword (t :: * threadfunc1) (long); t * m_obj; long m_param; threadfunc1 m_lpthreadfunc;};

This structure should be a member data as a CMYTHREAD. When CMYTHREAD creates a thread, the thread tone function is transmitted as a parameter, which is transparent to the user. At this time, the problem is coming, you must initialize this structure when you create a CMYTHREAD object, but this is not called Start, and I don't know the type of the specific call class, and this structure must be associated with the specific data type, CMYTHREAD initialization time What type of structure is there? Here, you will use a data type to force conversion skills. Let's first declare an empty class, take this class to initialize this structure:

Class nullclass {}; for convenience, TYPEDEF: TypeDef threadinfo Thinfo; this structure is: Class CMythread {... private: thinfo * m_info;}; Here you have additionally, under Borland C 5.02, this NULLCLASS only needs to declare, you don't need to implement, you can not add {}: class nullclass; it is, but VC6 is not, add {}, its sizeof (nullclass) is not the same, add {} The size returned is right, I don't know which company is wrong. It was found that there is no, this M_INFO structure has a dword nullclass :: threadfunc1) (long); function pointer, and this nullclass does not have this function, it doesn't matter, this NULLCLASS is just used to do data type conversion, and the band is also converted to member functions. Pointer type. For the sake of convenience, then Typedef This member function pointer: typedef threadinfo :: threadfunc1 threadfunc; this start can be implemented: Class CMythread {public: cmythread (); ~ cMythread ();

Template Bool Start (TName * Obj, ThreadFunc Func, Long Param = 0) {if (m_handle || obj == null || func == null) Return False; // Existing thread operation

ThreadInfo * lpv = new ThreadInfo ; lpv-> m_obj = obj; lpv-> m_param = param; lpv-> m_lpthreadFunc = (ThreadInfo :: THREADFUNC1) func; m_info = (ThInfo *) lpv DWORD GDWTHREADID = 0; m_handle = Createthread (null, 0, (lpthread_start_routine) ThreadProc, M_INFO, 0, & GDWTHREADID); return (bool) m_handle;} ...

Private: Static DWORD WINAPI ThreadProc (Thinfo * Obj);

PRIVATE: HANDLE M_HANDLE; Thinfo * m_INFO;

ThreadProc is very simple:

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

New Post(0)