From: http://tech.e800.com.cn/articles/2009/1126/1259215502260_1.html
Recently, I need to write something about a project.Program, It is annoying to write. Net, which always needs to carry the. NET package, so we are going to use VC ++ for writing. Here we use thread knowledge to record it for later use.
It is very easy to create a working thread. It takes only two steps for your thread to run: (1) implementing the thread function and (2) starting the thread. You can use cwinthread without modifying it. Next let's take a look at how to start a thread.
Afxbeginthread can be used to create a user interface thread or a worker thread. To start executing your thread, you only need to provide the following parameters to afxbeginthread:
Thread function address
Parameters transmitted to the thread function
(Optional) thread priority. The default priority is normal. If you want to use another priority, see setthreadpriority.
(Optional) the stack size of the thread. The default size is the same as the size of the stack for creating the thread.
(Optional) if the thread created by the user is suspended at the beginning, but not in the running state, it can be set to create_suincluded.
(Optional) security attributes of the thread. The default value is the same as the access permission of the parent thread. For details about the security information format, see security_attributes
Afxbeginthread creates and initializes a cwinthread object for the user, runs the object, and returns its address, so that the user can find it through this address. In this process, many checks are required. You don't have to worry about this. Let's take a look at how to write a thread function. The thread function defines what the thread is going to do. The thread starts when it enters the function and ends when it exits. This function must be in the following format:
Uint mycontrollingfunction (lpvoid pparam );
The parameter is a 32-digit number. This parameter is transmitted to the constructor of the object when the thread object is created. As for how the thread function processes this number, it can be a person's age, a file address, or a window handle, whatever it is, you have the initiative in your hands. If a parameter refers to a structure, the end can be used to send parameters to the thread, or the thread can send the results back to the main program. The thread needs to notify the main program when to get the results.
At the end of the thread function, a uint type value should be returned, indicating the reason for the return, that is, the returnCode . Normally, this number is 0, indicating that a normal response is returned. Of course, you can also define an error code indicating an error. As for what number represents, it's all about you. The following is an example of a thread function. This example explains how to define a thread function and how to control the thread from other places in the program. (To understand the whole thread, please read other Article )
uint mythreadproc (lpvoid pparam) {cmyobject * pobject = (cmyobject *) pparam; if (pobject = NULL |! Pobject-> iskindof (runtime_class (cmyobject) Return-1; // illegal parameter // do something with 'podobject' return 0; // thread completed successfully }...... // inside a different function in the program pnewobject = new cmyobject; afxbeginthread (mythreadproc, pnewobject); The above example is written on msdn. Below I also write a simple, the main function Passes parameters to the thread function, and the return value of the thread function. Main function: void cnewdlg: createnewthread () {cstring S = "hello"; afxbeginthead (workthread, & S ); // here, the parameter & S can be changed to another type of address you need to implement your operation. Sleep (1000); MessageBox (S, null, 0);} thread function: uint workthread (lpvoid pparam) // I think the key is this parameter, which can pass any type. {Cstring * S1 = (cstring *) pparam; // No matter what type you pass, you can use this method after conversion. * S1 = "s has been changed! "; Return 0;} Save and run. You will find that S has been changed to" s has been changed! ".