Multithreading development in the second--MFC of multithread programming

Source: Internet
Author: User

MFC's support for multithreaded programming

MFC has two types of threads, called worker threads and user interface threads, respectively. The main difference is that worker threads have no message loops, while user interface threads have their own message queues and message loops.

Worker threads do not have a message mechanism that is typically used to perform background computing and maintenance tasks, such as lengthy calculation procedures, printer spooling, and so on. User interface threads are typically used to handle user input that is independent of other thread execution, responding to events and messages generated by users and systems. But for WIN32 API programming, there is no difference between the two threads, and they all need the thread's startup address to start the thread to perform the task.

In MFC, the Global Function AfxBeginThread () is typically used to create and initialize the running of a thread, which has two overloaded forms for creating worker and user interface threads, respectively. The two types of overloaded function prototypes and parameters are described separately:

(1) CWinThread* AfxBeginThread(AFX_THREADPROC pfnThreadProc,
           LPVOID pParam,
           nPriority=THREAD_PRIORITY_NORMAL,
           UINT nStackSize=0,
           DWORD dwCreateFlags=0,
           LPSECURITY_ATTRIBUTES lpSecurityAttrs=NULL);
Pfnthreadproc: A pointer to the execution function of a worker thread, the thread function prototype must be declared as follows: UINT ExecutingFunction(LPVOID pParam); Note that executingfunction () should return a value of UINT type to indicate the reason for the end of the function. In general, a return of 0 indicates successful execution.

Pparam: A 32-bit argument passed to the thread function that the execution function interprets the value in some way. It can be a numeric value, or a pointer to a structure, or even be ignored;

Npriority: The priority level of the thread. If 0, the thread has the same priority as its parent thread;

Nstacksize: The thread allocates the size of the stack to itself, in bytes. If the nstacksize is set to 0, the thread's stack is set to the same size as the parent thread stack;

Dwcreateflags: If 0, the thread starts executing immediately after it is created. If create_suspend, the thread is suspended immediately after it is created;

Lpsecurityattrs: A thread's security attribute pointer, typically null;

(2) CWinThread* AfxBeginThread(CRuntimeClass* pThreadClass,
           int nPriority=THREAD_PRIORITY_NORMAL,
           UINT nStackSize=0,
           DWORD dwCreateFlags=0,
           LPSECURITY_ATTRIBUTES lpSecurityAttrs=NULL);

Pthreadclass is a pointer to the Run-time class object of an exported class of CWinThread that defines the start, exit, and so on of the created user interface thread, and other parameters of the same form 1. The thread generated by using this prototype of the function also has a message mechanism, and in future examples we will find almost the same mechanism as the main thread.

The following is a brief description of the data members of the CWinThread class and the commonly used functions.

M_hthread: The handle of the current thread;

M_nthreadid: ID of the current thread;

m_pMainWnd: Pointer to application main window

BOOL CWinThread::CreateThread(DWORD dwCreateFlags=0,
UINT nStackSize=0,
LPSECURITY_ATTRIBUTES lpSecurityAttrs=NULL);

The Dwcreateflags, nstacksize, lpsecurityattrs parameters in this function and the corresponding parameters in the API function CreateThread have the same meaning, the function succeeds, returns a value other than 0, or returns 0.

In general, call AfxBeginThread () to create and start a thread one at a time, but you can also create a thread in two steps: first create an object of the CWinThread class, and then call the object's member function CreateThread () to start the thread.

virtual BOOL CWinThread::InitInstance();

Overload this function to control the initialization of the user interface thread instance. A non 0 value is returned if the initialization succeeds, otherwise 0 is returned. User interface threads often overload this function, and worker threads generally do not use InitInstance ().virtual int CWinThread::ExitInstance();

Overload the function before the thread ends to perform some necessary cleanup work. The function returns the exit code for the thread, 0 for execution success, and a non-0 value to identify the various errors. Like the InitInstance () member function, this function applies only to user interface threads.

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.