"Convert" Windows API one-day training (45) createevent and setevent Functions

Source: Internet
Author: User

From: http://www.ecjtu.org/forum/read.php? Tid = 18641

 

When you create a thread, the thread is actually a loop, not running only once as above. This poses a problem. In that endless loop, we need to find the appropriate condition to exit the endless loop. How can we implement it? In Windows, events are often used. Of course, other methods can be used. Here, we will first introduce how to use events to notify the function to exit from the thread running function. The implementation principle is as follows. In that endless loop, the waitforsingleobject function is constantly used to check whether the event meets the requirements, if yes, exit the thread. If not, continue running. When running a blocking function in the thread, you must first change the blocking status to non-blocking when exiting the thread. For example, you can use a thread to receive network data, when using a blocked socket, you must first disable the socket and then send an event signal to exit the thread. The following example shows how to use an event to notify the thread to exit.

The createevent function declaration is as follows:

Winbaseapi
_ Out
Handle
Winapi
Createeventa (
_ In_opt lpsecurity_attributes lpeventattributes,
_ In bool bmanualreset,
_ In bool binitialstate,
_ In_opt lpcstr lpname
);
Winbaseapi
_ Out
Handle
Winapi
Createeventw (
_ In_opt lpsecurity_attributes lpeventattributes,
_ In bool bmanualreset,
_ In bool binitialstate,
_ In_opt lpcwstr lpname
);
# Ifdef Unicode
# Define createevent createeventw
# Else
# Define createevent createeventa
# Endif //! Unicode
Lpeventattributes is the attribute of an event.
Bmanualreset indicates whether the event is manually reset or automatically reset.
Binitialstate indicates whether the initial state is in a signal state.
Lpname is the name of the event. If there is a name, you can share the event status across processes.

An example of calling this function is as follows:
#001 # pragma once
#002
#003 // Thread class.
#004 // Cai junsheng 2007/09/23 QQ: 9073204
#005 class cthread
#006 {
#007 public:
#008
#009 cthread (void)
#010 {
#011 m_hthread = NULL;
#012 m_heventexit = NULL;
#013}
#014
#015 virtual ~ Cthread (void)
#016 {
#017 if (m_hthread)
#018 {
#019 // The deleted thread resource.
#020: closehandle (m_hthread );
#021}
#022
#023 if (m_heventexit)
#024 {
#025 // delete an event.
#026: closehandle (m_heventexit );
#027}
#028
#029}
#030
#031 // create a thread
#032 handle createthread (void)
#033 {
#034 // create an exit event.
#035 m_heventexit =: createevent (null, true, false, null );
#036 if (! M_heventexit)
#037 {
#038 // An error occurred while creating the event.
#039 return NULL;
#040}
#041
#042 // create a thread.
#043 m_hthread =: createthread (
#044 null, // use the default security attribute.
#045 0, // The stack size of the thread.
#046 threadproc, // address of the thread running function.
#047 this, // The parameter passed to the thread function.
#048 0, // create a flag.
#049 & m_dwthreadid); // ID of the created thread.
#050
#051 return m_hthread;
#052}
#053
#054 // wait for the thread to end.
#055 void waitfor (DWORD dwmilliseconds = infinite)
#056 {
#057 // send the exit thread signal.
#058: setevent (m_heventexit );
#059
#060 // wait for the thread to end.
#061: waitforsingleobject (m_hthread, dwmilliseconds );
#062}
#063
#064 protected:
#065 //
#066 // run the function in a thread.
#067 // Cai junsheng 2007/09/21
#068 //
#069 static DWORD winapi threadproc (lpvoid lpparameter)
#070 {
#071 // convert the input parameters.
#072 cthread * pthread = reinterpret_cast <cthread *> (lpparameter );
#073 if (pthread)
#074 {
#075 // The thread return code.
#076 // call the thread processing function of the class.
#077 return pthread-> Run ();
#078}
#079
#080 //
#081 return-1;
#082}
#083
#084 // run the function in a thread.
#085 // here, you can use the members in the class or make the derived class more powerful.
#086 // Cai junsheng 2007/09/25
#087 virtual DWORD run (void)
#088 {
#089 // output to the debugging window.
#090: outputdebugstring (_ T ("Run () thread function run \ r \ n "));
#091
#092 // thread loop.
#093 (;;)
#094 {
#095 DWORD dwret = waitforsingleobject (m_heventexit, 0 );
#096 if (dwret = wait_timeout)
#097 {
#098 // you can continue running.
#099 tchar chtemp [0, 128];
#100 wsprintf (chtemp, _ T ("threadid = % d \ r \ n"), m_dwthreadid );
#101: outputdebugstring (chtemp );
#102
#103 // if nothing is done at present, let the thread release the CPU.
#104 sleep (10 );
#105}
#106 else if (dwret = wait_object_0)
#107 {
#108 // exit the thread.
#109: outputdebugstring (_ T ("Run () Exit thread \ r \ n "));
#110 break;
#111}
#112 else if (dwret = wait_abandoned)
#113 {
#114 // error.
#115: outputdebugstring (_ T ("Run () thread error \ r \ n "));
#116 return-1;
#117}
#118}
#119
#120 return 0;
#121}
#122
#123 protected:
#124 handle m_hthread; // thread handle.
#125 DWORD m_dwthreadid; // thread ID.
#126
#127 handle m_heventexit; // thread exit event.
#128 };
#129

The preceding thread exit event is created in row 35th, row 95th checks whether the event can exit the thread operation, and row 58th sets the thread exit event.

Related Article

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.