Recently I have a special post on thread-related thread. I learned a lot. below is the Post URL
Http://topic.csdn.net/u/20090812/20/0379085f-7145-4bac-81bf-742114335d69.html
Sort out what you learned.
(Abstract)
Waitforsingleobject usage
Waitforsingleobject usage
DWORD waitforsingleobject (handle hhandle, DWORD dwmilliseconds );
The hhandle parameter is the handle of an event, and the second parameter dwmilliseconds is the time interval. If the time is in the signal state, wait_object_0 is returned. If the time exceeds the dwmilliseconds value but the time event is still in the non-signal state, wait_timeout is returned.
Hhandle can be the handle of the following objects:
Change Notification
Console input
Event
Job
Memory resource notification
Mutex
Process
Semaphore
Thread
Waitable Timer
The waitforsingleobject function is used to detect the signal status of the hhandle event. When the function execution time exceeds dwmilliseconds, it returns. However, if the dwmilliseconds parameter is infinite, the function returns only when the corresponding time event changes to a signal state, otherwise, wait until waitforsingleobject has a returned value before executing the subsequent code. In addition, when dwmilliseconds is set to a special value of 0, test whether the hhandle core object is fired and the function returns immediately.
Return Value:
Wait_abandoned:When the hhandle is mutex, if the thread with mutex does not release the core object at the end, the returned value is returned.
Wait_object_0: The core object has been activated.
Wait_timeout: Wait for timeout
Wait_failed: An error occurs. You can use getlasterror to get the error code.
Here is an example:
First create a global event object g_event:
Cevent g_event;
In the program, you can call cevent: setevent to set the event to a signal state.
Below is a thread function mythreadpro ()
Uint cflushdlg: mythreadproc (lpvoid pparam)
{
Waitforsingleobject (g_event, infinite );
For (;;)
{
.............
}
Return 0;
}
In this thread function, the following for loop is executed only when g_event is set to a signal state. Because g_event is a global variable, we can use g_event in other threads. setevent controls this thread.
Another method is to use the waitforsingleobject function to execute a thread function at intervals.
Uint cflushdlg: mythreadproc (lpvoid pparam)
{
While (waitforsingleobject (g_event, mt_interval )! = Wait_object_0)
{
..................
}
Return 0;
}
In this thread function, you can set mt_interval to control how often the function body of this thread is executed. When the event is in a non-signal state, the function is executed once every mt_interval, when the event is set to a signal state, the thread completes execution.
Setevent cevent: setevent
Bool setevent ();
Return Value: if the operation is successful, a non-zero value is returned. Otherwise, the value is 0.
Note:
Set the event status to marked to release any waiting thread. If the event is manual, the event remains tagged until resetevent is called. In this case, multiple threads will be released. If the event is automatic, the event will remain tagged until a thread is released, and the system will set the event status to unlabeled. If no thread is waiting, the event remains tagged until a thread is released. (Reprinted)
Cevent class
The cevent class provides event support. An event is a synchronization object that allows a thread to wake up in a certain situation. The event tells the thread when to execute a given task, so that the stream of multiple threads is smooth. For example, in some network applications, one thread (as a) is responsible for listening to the communication port, and the other thread (as B) is responsible for updating user data. By using the cevent class, thread a can notify thread B when to update user data, so that thread B can update user data as soon as possible. Each cevent object can have two states: signaled and nonsignaled ). The thread monitors the status of the cevent Class Object in it, and takes corresponding actions accordingly. In MFC, there are two types of cevent objects: manual events and automatic events. For an automatic event, when it receives a signal, it will release the next available thread. An automatic cevent object is automatically returned to the non-signal state after at least one thread is released. After the human event object obtains the signal, it releases all available threads until it calls the member function resetevent () set it to the signal-free status. Note: When you create a cevent Class Object, automatic events are created by default. The prototype and parameters of the member functions of cevent are described as follows. Cevent (bool binitiallyown = false, bool bmanualreset = false, lpctstr lpszname = NULL, lpsecurity_attributes lpsaattribute = NULL ); Binitiallyown: If binitiallyown is set to true, the threads of the cmultilock Class Object and csinglelock class object are available. Otherwise, the threads that need to access resources must wait. The default value of this parameter is false. Bmanualreset: Specifies whether the cevent object to be created is a manual event or an automatic event. If it is true, it is a manual event; otherwise, it is an automatic event. The default value is false. Lpszname: Specifies the name of the event object to be created. If the event object is used across processes, this parameter cannot be null. If this parameter is the same as an existing cevent object, the constructor returns a reference to this existing object; if the parameter is the same as an existing non-cevent synchronization object (such as cmutex), the object creation fails. lpsaattribute: pointer to the security_attributes structure, this parameter determines the Security Attribute of the event object to be created. It is usually set to null. After an event object is built, you can call its member functions to change its status. Bool cevent: setevent (); Sets the status of the cevent class object to a signal state and releases all pending threads. If this event is a manual event, the cevent class object remains in a signal state, when the member function resetevent () is called to reset it to the stateless state, this event can release multiple threads. If the cevent class object is an automatic event, after the setevent () event is set to a signal state, the cevent class object is automatically reset from the system to a signal-free State, unless a thread is released. If the function is successfully executed, a non-zero value is returned. Otherwise, zero is returned. Bool cevent: resetevent (); This function sets the event status to stateless, and maintains the status until setevent () is called. Because automatic events are automatically reset by the system, automatic events do not need to call this function. If the function is successfully executed, a non-zero value is returned. Otherwise, a non-zero value is returned. Bool cevent: pulseevent () Sends an event pulse, Which is returned after the function completes a series of operations. For automatic events, pulseevent () sets the event to a signal state, waits for a thread to be released, resets the event to a non-signal state, and then returns the pulseevent (). For manual events, all threads waiting for the event are released, the event is automatically reset to the stateless state, and then the pulseevent () is returned. After a cevent object is created in the thread, it is automatically in the stateless state. However, in another thread, you can call the Win32 API waitforsingleobject () function to monitor its status. The prototype and parameters of the function are described as follows: DWORD waitforsingleobject (handle hhandle, DWORD dwmilliseconds ); Here, hhandle is the handle to the synchronization object to be monitored, and dwmilliseconds is the timeout value set for the object to which hhandle is monitored, in milliseconds. When this function is called in the thread's execution function, the thread temporarily suspends. The system monitors the state of the object indicated by hhandle. If the object indicated by hhandle changes to a signal state after dwmilliseconds milliseconds, waitforsingleobject () is returned, the thread is released, and the return value is wait_timeout; If the object waiting by the thread changes to a signal at a certain time point within the suspended dwmilliseconds millisecond, the function returns immediately and the return value is wait_object_0. The dwmilliseconds parameter has two special values: 0 and infinite. If the value is 0, the function returns immediately. If the value is infinite, the thread is suspended until the object indicated by hhandle changes to a signal state. If the cevent object is an automatic event, when waitforsingleobject (hhandle, infinite) returns, the cevent object is automatically reset to the signal-free state. Cevent: setevent () sets the object to a signal State to release the waiting thread. Cevent: resetevent () sets the object to a stateless state, and the program waits at waitforsingleobject (hhandle, infinite. Routine 9 multithread9 creates a project multithread9 Based on the dialog box. In the idd_multithread9_dialog dialog box, add a button and two edit box controls. The button ID is idc_writew and the title is "Write 'W '"; the IDS of the two edit boxes are idc_w and idc_d, And the read-only attribute is selected; Declare two thread functions in the multithread9dlg. h file: Uint writew (lpvoid pparam ); Uint writed (lpvoid pparam ); Use classwizard to add the cedit class variables m_ctrlw and m_ctrld to idc_w and idc_d respectively; Add the following content to the multithread9dlg. cpp file: To correctly use the synchronization class in the file, add # include "afxmt. H" at the beginning of the file" Defines the event object and a character array. To be used between different threads, it is defined as a global variable. Cevent eventwrited; Char g_array [10]; Add a thread function: Uint writew (lpvoid pparam) { Cedit * pedit = (cedit *) pparam; Pedit-> setwindowtext (""); For (INT I = 0; I <10; I ++) { G_array [I] = ''w ''; Pedit-> setwindowtext (g_array ); Sleep (1000 ); } Eventwrited. setevent (); Return 0 ;} Uint writed (lpvoid pparam) { Cedit * pedit = (cedit *) pparam; Pedit-> setwindowtext (""); Waitforsingleobject (eventwrited. m_hobject, infinite ); For (INT I = 0; I <10; I ++) { G_array [I] = ''d ''; Pedit-> setwindowtext (g_array ); Sleep (1000 ); } Return 0 ;} By carefully analyzing these two thread functions, you will understand the cevent class correctly. The thread writed executes to waitforsingleobject (eventwrited. m_hobject, infinite); wait until the event eventwrited is a signal and the thread is executed. Because the eventwrited object is an automatic event, when waitforsingleobject () returns, the system automatically resets the eventwrited object to the stateless state. Double-click the idc_writew button to add the response function: Void cmultithread9dlg: onwritew () { Cwinthread * pwritew = afxbeginthread (writew, & M_ctrlw, Thread_priority_normal, 0, Create_suincluded ); Pwritew-> resumethread (); cwinthread * pwrited = afxbeginthread (writed, & M_ctrld, Thread_priority_normal, 0, Create_suincluded ); Pwrited-> resumethread ();} Compile and run the program, and click the write 'W' button to understand the role of the event object. |