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
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
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
Template
ThreadInfo
Private: Static DWORD WINAPI ThreadProc (Thinfo * Obj);
PRIVATE: HANDLE M_HANDLE; Thinfo * m_INFO;
ThreadProc is very simple: