"Sync" Xinxin Sun VC + + Notes-Event object synchronization

Source: Internet
Author: User

Event Object Synchronization

//the CreateEvent is set to custom, and the initial signal is #include<windows.h>#include <iostream.h>DWORD WINAPI ThreadProc1 (lpvoid lpparameter);D word WINAPI Threadpro C2 (LPVOID lpparameter);inttickes= -; HANDLE g_hevent;intMain () {HANDLE hThread1=createthread (NULL,0, Threadproc1,null,0, NULL); HANDLE hThread2=createthread (NULL,0, Threadproc2,null,0, NULL); G_hevent=createevent (Null,flase,true,null);//automatic, with signalCloseHandle (HTHREAD1);    CloseHandle (HTHREAD2); Sleep (4000); return 0; }dword WINAPI ThreadProc1 (lpvoid lpparameter)//Thread Data                {     while(TRUE) {WaitForSingleObject (g_hevent,infinite); if(tickes>0) {Sleep (1); cout<<"thread1 sell Ticke:"<<tickes--<<Endl; }Else{             Break;    } SetEvent (G_hevent); }    return 0; }dword WINAPI ThreadProc2 (lpvoid lpparameter)//Thread Data{     while(TRUE) {WaitForSingleObject (g_hevent,infinite); if(tickes>0) {Sleep (1); cout<<"thread2 sell Ticke:"<<tickes--<<Endl; }Else{             Break;    } SetEvent (G_hevent); }    return 0;}//The conclusion is that the setting is automatic and the signal is started (no signal can be called setevent to make a transition without signal to a signal)//only one thread gets the signal, and when it's finished (the time slice is up), the next thread cannot run because there is only one//when the thread is signaled and runs out, it immediately makes the event object non-signaled
HANDLE WINAPI createevent (  _in_opt_ lpsecurity_attributes lpeventattributes,  _in_     BOOL                  bManualReset,  _in_     BOOL                  binitialstate,  _in_opt_ lpctstr               lpname);

 lpeventattributes[Enter] A pointer to the SECURITY_ATTRIBUTES structure to determine whether the returned handle can be inherited by the quilt process. If lpeventattributes is null, this handle cannot be inherited. Windows NT/ -: Members in the lpeventattributes structure specify a security character for the new event. If lpeventattributes is null, the event obtains a default security character.
bManualReset [input] Specifies whether the event object is created to be manually restored or automatically restored. If true, you must use the ResetEvent function to manually restore the state of the event to a signal-free state. If set to false, when a waiting thread is freed, the system automatically reverts the event state to a no-signal state.
binitialstate [input] Specifies the initial state of the event object. If true, the initial state is signaled, otherwise there is no signal state.
lpname [Enter] The name of the object that specifies the event, which is a string pointer that ends with 0. The character format of the name is limited to MAX_PATH. The name is case-sensitive. If the name specified by Lpname is the same as the name of an existing named event object, the function requests event_all_access to access the existing object. At this point, because the bManualReset and binitialstate parameters are already set in the process of creating the event, these two parameters are ignored. If Lpeventattributes is a parameter that is not NULL, it determines whether the handle can be inherited, but its security descriptor members are ignored. If lpname is null, an unnamed event object is created. If the lpname is the same as an existing signal, mutex, wait timer, job, or file-mapped object name, the function will fail and the Error_invalid_handle will be returned in the GetLastError function. This behavior is caused by the fact that these objects share a single namespace.

"Sync" Xinxin Sun VC + + Notes-Event object synchronization

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.