The C ++ class member method is directly used as a thread callback function.

Source: Internet
Author: User

When I used to write a thread, I either honestly follow the declaration or use the static member function of the C ++ class as the callback function. The encapsulation is often broken due to the thread code. although I knew the expansion form of class member functions before, I never thought about using it. I learned this trick when I went deep into ATL yesterday. :) the class member method is a special function, it will be converted into a common function during compilation. For example, the TMyClass class: class TMyClass {void Func () ;}; TMyClass :: func will eventually be converted to void Func (TMyClass * this); that is to say, insert the this pointer to the object itself before the original first parameter. We can use this feature to write a non-static class member method to directly act as a thread callback function. first look at the definition of the _ beginthread function: unsigned long _ RTLENTRY _ EXPFUNC _ beginthread (void (_ USERENTRY * _ start) (void *), unsigned _ stksize, void * _ arg ); the first parameter is the callback function used as the thread execution subject. Its prototype is void Func (void *). This void * parameter is passed in as custom data. Compare the final form of TMyClass: Func mentioned above, which can meet the requirements here. Now let's do an experiment: # include <stdio. h> # include <process. h> class TMyClass {int m_nCount; int m_nId; public: TMyClass (int nId, int nCount): m_nId (nId), m_nCount (nCount) {} void _ userthreadentry proc () // Class member method {for (int I = 0; I <m_nCount; I ++) // print a row of numbers based on m_nCount members {printf ("Class % d: % d \ n ", m_nId, I) ;}}; int main (int argc, char * argv []) {// Union class, it is used to convert the method pointer of a class member to a normal function pointer (the compiler does not allow forced conversion between the two functions). I wonder if there is any better method. Union {void (_ USERENTRY * ThreadProc) (void *); void (_ USERENTRY TMyClass: * MemberProc) ();} Proc; // although the two function types in the Union seem very different now, their final forms are the same. TMyClass MyClass1 (), MyClass2 (); // generate two TMyClass objects: Proc. memberProc = & TMyClass: ThreadProc; // conversion, Proc. threadProc is the corresponding normal function pointer _ beginthread (Proc. threadProc, 4096, & MyClass1); // start thread, Here Proc. threadProc is actually TMyClass: ThreadProc, And the this pointer it requires is what we give & MyClass1. _ Beginthread (Proc. ThreadProc, 4096, & MyClass2); system ("pause"); return 0 ;} run! Magic? :-) In fact, not only the thread callback function, but as long as the callback function is like Func (void *,...), you can use this method to directly use the class member method. (The premise is that the first void * is custom data, that is, it cannot have other functions ).

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.