Multi-thread programming in WINDOWS

Source: Internet
Author: User

Multithreading programming in WINDOWS is a very deep topic. Some people say that mastering multithreading is the difference between a programmer and an amateur! Today, I finally took back a book that I thought for a long time from the library. The name "multi-thread implementation in WINDOWS" is also a book that writes my own experience, I think I am very suitable for reading such books, but I don't like the routines. I always think that I can read simple things. He has to make something irrelevant to me, the status of the multi-thread subject is diminished. Therefore, I looked at MSDN again based on his ideas. Although it was simple, it was easy to understand and quickly applied it to my program design, I think there is no force to simply talk about multithreading. It is necessary to realize its advantages and troubles in practical applications. For example, it is used in network SOCKET programming. Let me talk about some basic concepts:
I. What are processes and threads:

A process is an example of an application execution. For example, when you double-click "Notepad", the process of running "Notepad" starts.

"Thread" is the execution path in the process. When notepad is started, the operating system creates a process and starts to execute the main thread of the process. The process is also terminated when this thread is terminated. The startup Code provides the main thread to the operating system in the form of a function address. It is usually the address of the provided main function or WinMain function.

However, you can also create sub-(other) threads under the main thread to complete some background processing work. You may want to perform this operation when you want to process background or maintenance tasks but do not want users to wait to complete these tasks. All threads in MFC are represented by CWndThread. However, in most cases, we do not need to explicitly create these objects. Instead, we can use the AfxBeginTread function to create a thread. Its definition is as follows: CWinThread * AfxBeginThread (
AFX_THREADPROC pfnThreadProc,
LPVOID pParam,
Int nPriority = THREAD_PRIORITY_NORMAL,
UINT nStackSize = 0,
DWORD dwCreateFlags = 0,
LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL
);
CWinThread * AfxBeginThread (
CRuntimeClass * pThreadClass,
Int nPriority = THREAD_PRIORITY_NORMAL,
UINT nStackSize = 0,
DWORD dwCreateFlags = 0,
LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL
);

There are two types of threads in MFC,
1. A user interface thread is used to complete work related to user input and respond to user events. To implement this function, there is often a mechanism called message pump. For example, WinApp is an instance of the user interface thread object, which is used as the main thread to process user messages.
2. The other is the auxiliary thread or the worker thread. It is mainly used to complete operations that do not require user input.
The following example shows how a worker thread is created.
1: Create an MFC project with a document/view structure. Add two items to the menu, one representing multithreading and the other representing a single thread;
2. Create the following class structure:

3: Create a subthread void CWorkThreadView: OnMThead () in OnMThead ()
{
// TODO: Add your command handler code here
PThread = AfxBeginThread (Calculate, this );
AfxMessageBox ("Haha, I am the main thread in multiple threads. You will see my child thread come out with me! ");
PSubThread = AfxBeginThread (SubThread, this );

}

4: The above SubThread is another processing function UINT SubThread (LPVOID pParam)
{
CWorkThreadView * pView = (CWorkThreadView *) pParam;
PView-> PostMessage (WM_SUBTHREADMSG, NULL, 0 );
Return 0;
}

5: The function used to process the WM_SUBTHREADMSG message in the CWorkThreadView class is: DealSubThreadMsgvoid CWorkThreadView: DealSubThreadMsg (WPARAM wParam, LPARAM lParam)
{
AfxMessageBox ("Haha, A subthread sent a message to me! ");
}

6: Now, you should guess the running result. Isn't it easy. Now, let's summarize how to create a working thread:
* Start the AfxBeginThread process.
* Implement control functions

Here is just a simple method to create a working thread. It can be said that it is just an entry. The essence of Multithreading is not here, but in subsequent thread communication, mutex access, semaphores, and other details, multithreading! I just got on the road!

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.