VC + + to achieve multithreading scheduling and processing

Source: Internet
Author: User

The WINDOWS95 and WindowsNT operating systems support multitasking and processing, and based on the multitasking space provided by the feature, programmers can fully control the operation of each fragment in the application to write efficient applications.

Multitasking usually includes two broad categories: multiple processes and multithreading. A process is an application that is running in the system; A thread is the basic unit of the system allocating processor time resources, or a unit that executes independently within a process. For the operating system, its dispatch unit is a thread. A process includes at least one thread, which is usually called the main thread. A process that starts with the execution of the main thread and then creates one or more additional threads is called multithreading based multitasking.

Development of multi-threaded applications can take advantage of the 32-bit Windows environment provided by the WIN32 API interface functions, can also use VC + + provided in the MFC class library for development. Multithreaded programming is the same principle in both ways, and users can choose the appropriate tools as needed. This paper focuses on using MFC class library provided by vc++5.0 to implement multithreading scheduling and processing, and synchronization multitask characteristics caused by thread multitasking, and finally explains a routine of multithread implementation in detail.

Two-threaded programming based on MFC

1 MFC support for multithreading

MFC class libraries provide multithreaded programming support and are more convenient for user programming implementations. It is very important that, in the case of multiple window threads, MFC provides the design of the user interface thread directly.

MFC distinguishes between two types of threads: worker thread (worker thread) and user interface thread (userinterface thread). Worker threads do not have a message mechanism and are typically used to perform background calculation and maintenance tasks. MFC provides a message mechanism for user interface threads to handle user input and respond to user-generated events and messages. But for the Win32 API, there is no difference between the two threads, and it only requires the thread's startup address to start the thread to perform the task. A typical application of user interface thread is class CWinApp, which is familiar to class CWinApp, it is a derived class of CWinThread class, the main thread of the application is provided by it, and it is responsible for handling the events and messages generated by the user. Class CWinThread is the basic class for user interface threads. CWinThread objects are used to maintain local data for a particular thread. Because processing thread local data depends on the class CWinThread, all threads that use MFC must be created by MFC. For example, a thread created by the Run-time function _beginthreadex cannot use any MFC APIs.

2 creation and termination of worker threads and user interface threads

To create a thread, you need to call the function AfxBeginThread. The function has two versions, corresponding to the worker thread and the user interface thread, through parameter overloading. Whether it's a worker thread or a user interface thread, you need to specify additional parameters to modify the priority, stack size, create flags, and security features. function AfxBeginThread Returns a pointer to the CWinThread class object.

Creating an assistant thread is relatively straightforward. Only two steps are required: implementing the control function and starting the thread. It does not have to derive a class from CWinThread. The brief description is as follows:

1. Implement the control function. The control function defines the thread. When the function is entered, the thread starts, and when it exits, the thread terminates. The control function is declared as follows:

UINT mycontrollingfunction (LPVOID pparam);

The parameter is a single-precision 32-bit value. The value received by this parameter passes the thread object to the constructor when it is created. The control function will interpret the value in some way. Can be a quantity value, or a pointer to a structure that includes multiple parameters, and can even be ignored. If the argument refers to a struct, you can pass data not only from the calling function to the thread, but also from the thread back to the calling function. If you use such a structure to return data, the thread notifies the calling function when the result is ready. When the function ends, a value value of the UINT type should be returned, indicating the reason for the end. Typically, a return of 0 indicates success, and other values represent different errors.

2. Start the thread. The object of a CWinThread class is created and initialized by the function AfxBeginThread and the address of the thread is started and returned. The thread enters the running state.

3. Examples are provided. Here's a simple code that shows how to define a control function and how to use it in other parts of the program.

UINT MyThreadProc( LPVOID pParam )
{
CMyObject* pObject = (CMyObject*)pParam;
if (pObject == NULL ||
!pObject- >IsKindOf(RUNTIME_CLASS(CMyObject)))
return -1; //非法参数
……//具体实现内容
return 0; //线程成功结束
}
//在程序中调用线程的函数
……
pNewObject = new CMyObject;
AfxBeginThread(MyThreadProc, pNewObject);
……

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.