Afxbeginthread in-depth analysis

Source: Internet
Author: User

I have read the in-depth introduction to MFC, J. j. hou mentioned that it is best not to use createthread directly when creating a new thread. Although afxbeginthread is also an encapsulation of createthread, The afxbeginthread API does a lot of basic work for creating threads: call cwinthread * pthread = (cwinthread *) pthreadclass-> Createobject (), use the C ++ Runtime Library to create class objects and get object pointers.

Note that the obtained pointer is forcibly converted to cwinthread * and the pointer is returned. Therefore, we can forcibly convert the pointer back when receiving the pointer, because the memory model is our cwinthread derived class, there will be no errors, and you can call the methods of this derived class and access resources and other operations. For example, to call the postthreadmessage () method of the cwinthread derived class object, the premise is that this thread is a UI thread, and it is unnecessary if it is a worker thread. On_thread_message () is used to receive messages. Of course, you can capture messages sent by other threads in pretranslatemessage.

 

 

Let's take a look at the inheritance diagram of the class:

  

 

We can see that the cwinthread class inherits from the cwintarget and cwinthread derives from the cwinapp. We know that one application contains only one cwinapp, which represents the application itself, but it does not mean that an application can only have one cwinthread. Multiple cwinthreads represent multiple threads. Yes, cwinthread represents one thread. When our application requires multiple threads, We need to generate a cwinthread object and save its pointer. However, ensure that only one cwinapp can be created. In addition, all classes inherited from cve-target have the ability to process command messages: menu clicks, acceleration keys, and other messages. All classes inherited from cobject have the following capabilities: dynamically generate objects and serialization.

After all, a UI thread derived from cwinthread is no different from our main thread (the thread where cwinapp is located). It also has a message loop, the thread must be baptized by virtual bool initinstance (); before it starts. Of course we can do some initialization work in this function, just like cwinapp.

  

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.