Introduction/Basic usage of C + + AfxBeginThread

Source: Internet
Author: User

AfxBeginThread
Both the user interface thread and the worker thread are created by AfxBeginThread. Now, examine the function: MFC provides two overloaded versions of AfxBeginThread, one for the user interface thread, and the other for worker threads, with the following prototypes and procedures:

AfxBeginThread of user interface threads
The AfxBeginThread prototype of the user interface thread is as follows:

cwinthread* AFXAPI AfxBeginThread (CRuntimeClass* pthreadclass,int  npriority,uint Nstacksize,dword dwcreateflags,lpsecurity_attributes lpsecurityattrs)
which
The parameter 1 is the Runtime_class class derived from CWinThread;
Parameter 2 specifies the thread priority and, if 0, the same thread as the thread that created it;
Parameter 3 Specifies the stack size of the thread, and if 0, the same thread as the thread that created it;
The parameter 4 is a create identity, and if it is create_suspended, the thread is created in the suspended state and the thread is suspended after the thread has been created, otherwise the threads are executed after creation.
The parameter 5 represents the thread's security properties, which are useful under NT.

Worker thread AfxBeginThread
The prototype of the worker thread AfxBeginThread is as follows:

cwinthread* AFXAPI afxbeginthread (afx_threadproc pfnthreadproc,lpvoid pparam,int  npriority, UINT Nstacksize,dword dwcreateflags,lpsecurity_attributes lpsecurityattrs)
which
Parameter 1 The entry function of the thread, the declaration must be as follows: UINT mythreadfunction (LPVOID pparam);
Parameter 2 is passed into the thread's arguments, note that it is of type: LPVOID, so we can pass a struct into the thread.
Parameters 3, 4, and 5 Specify the priority of the thread, the stack size, the creation identity, the security attribute, and the meaning of the user-interface thread, respectively.

Appendix A:

Two ways to end a thread
When you are using threads to print some graphics in the background. Sometimes after you print a part, you want to be able to stop, so how do you let the thread stop it.

The polygon will explain to you in detail two ways to end a thread
1: This is the simplest way, that is, to let the thread function perform, at which point the thread ends normally. It returns a value, generally 0 is the successful end,

Of course you can define your own values that you think are appropriate to execute successfully on behalf of the thread. Calling AfxEndThread within a thread will end the threads directly, and all the resources on the thread will be recycled.
2: If you want a thread B to end thread A, then you need to pass the information in these two threads. Whether it's a worker thread or an interface thread, if you want it to end after a thread is finished, you can invoke:: GetExitCodeThread function

Or the teacher's project, previously due to the computational volume is too large, causing the program to often appear suspended animation phenomenon, because the program has only one thread, the thread is mainly used for processing computations, and the response to Message Queuing is ignored. So the solution is to use two threads, one thread for computation, and one thread for processing messages.
To find some information on the Internet, found in MFC to divide threads into two categories, one for the interface thread, and one for the worker thread. The difference between the two is that the message response can be handled before, while the latter cannot. For this project, just put the calculation process in a working thread to do it.
Now try it, I've created a new dialog box with two buttons on it, one for start and the other for dialog. The former is used to start the calculation, while the latter pops up a message box. Then add a dead loop function to the dialog box.
UINT Cmultithreaddlg::jisuan (LPVOID lpparam)
{
int i = 1;
for (;;)
{
I+=i;
}
return 0;
}
Then add Jisuan (NULL) on the response function of the Start button, and now run the program, press the Start button, you can see the CPU usage up to 100%, and then press the dialog button no response, drag the closing window is not valid. This is the earlier mention of the phenomenon of suspended animation (in fact, the dead, because the cycle of death, if not the cycle of death, but only the amount of computation is too large is suspended animation).

The following is a multi-threaded approach to solve, the Start button response function changed to

cwinthread* mythread = AfxBeginThread (   Jisuan,   NULL,   thread_priority_normal,   0 ,    0 ,   NULL   );
Run, results found error C2665: ' AfxBeginThread ': None of the 2 overloads can convert parameter 1 from type ' unsigned int (void *) ' Generating Code ...
I'm just wondering, the function pointer is right, The original thread function can and must be a global function or a static member function.
So we change the declaration of the thread function to the static UINT Jisuan (LPVOID lpparam), then run the program, click Start, when the CPU up to 100%, click Dialog, pop Up the dialog box, drag, close the window is no problem.

In fact, the above AfxBeginThread, in addition to the first two parameters, the following are the default parameters, can be omitted. The two parameters must be, one is a pointer to the thread function, and the other is the argument passed to the function. In practice we often use AfxBeginThread (threadproc,this), or//to pass this to the members of the class can be called. This allows the thread function to use and manipulate members of the class. It is important to note that a thread function is a static class function member.

The thread is created, but what if you want to pause it halfway?
We get a cwinthread pointer when we create the thread, a pointer to the thread object, a function of pausing and resuming in the CWinThread class, and I'll show you the following.
Make changes on the original program. Add a cwinthread* member variable to the dialog class without initializing it to NULL, which will cause an error because it can only be obtained through the AfxBeginThread function. Remove the declaration from start.
Then add a pause button to its response function to add code mythread->suspendthread (); Add a Resume button and add Mythread->resumethread () to its response function;
Run the program again, after we start, press pause to see the CPU back to normal, and then resume,cpu up again, to prove that everything is working properly.

Introduction/Basic usage of C + + AfxBeginThread

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.