VC + + Thread Sync-Event Object __c++

Source: Internet
Author: User

This is the collation of Sun Xin VC received on the thread synchronization notes.

The N event object also belongs to the kernel object, contains a usage count, a Boolean value that indicates whether the event is an automatic reset, an event that is manually reset, and a Boolean value that indicates whether the event is in a notified state or an unannounced state.

N has two different types of event objects. One is an event that is manually reset, and the other is an event that is automatically reset. When an event that is manually reset is notified, all threads waiting for the event become a scheduled thread. When an automatically reset event is notified, only one thread in the thread waiting for the event becomes a scheduled thread. After you get the event object, the operating system sets the event object as not a signal state because it is an automatically reset event object

---------------------------------------------------The following manual reset event objects have no practical reference value------------------------------------------------- ---------

#include <windows.h>

#include <iostream.h>

DWORD WINAPI Fun1proc (

LPVOID lpparameter//thread data

);

DWORD WINAPI Fun2proc (

LPVOID lpparameter//thread data

);

int tickets=100;

HANDLE g_hevent; A global handle variable that holds the handle to the event object that is created. Because more than one thread accesses the G_hevent object, it is set as a global variable.

void Main ()

{

HANDLE HThread1;

HANDLE hThread2;

Hthread1=createthread (Null,0,fun1proc,null,0,null);

Hthread2=createthread (Null,0,fun2proc,null,0,null);

CloseHandle (HTHREAD1);

CloseHandle (HTHREAD2);

G_hevent=createevent (Null,true,false,null); The third status is false to indicate that the event was initialized to a no signal state at creation time

/* (1) The second parameter sets true to manually reset the event object, and when the manually reset event is notified, all threads waiting for the event become a scheduled thread. That means they can all run at the same time. Two threads can run at the same time that the synchronization failed. The ResetEvent (g_hevent) manually sets the event object to a non signal state, and the manual reset must have an explicit setevent setting signaled. (2) The second parameter is set to False to automatically reset the event object, and when an automatically reset event is notified, only one thread in the thread waiting for the event becomes a scheduled thread, and the event object is set to be not signaled. Try not to manually reset the event object, because the following problem occurs

*/

SetEvent (g_hevent)//sets the event object to a signaled state, but at this point all threads become signaled, so each thread needs to be preceded by a resetevent

Sleep (4000);

CloseHandle (g_hevent);

}

DWORD WINAPI Fun1proc (

LPVOID lpparameter//thread data

)

{

while (TRUE)

{

WaitForSingleObject (G_hevent,infinite);

ResetEvent (g_hevent);  Manually reset the event object unless the display calls ResetEvent (g_hevent); Set event object to no signal state, it will always be in a signaled state

/*

It is not possible to use resetevent to set the preservation object to a non signal state because it has two problems:

1 under the single CPU platform, only one thread can run at the same time. Suppose thread 1 runs WaitForSingleObject (g_hevent,infinite) First, it passes WaitForSingleObject (G_hevent,infinite), and the event object is g_hevent. But at this time its time slice terminates and the second thread runs. Because the resetevent of thread 1 is not executed, all our event objects are still in a signaled state. Since it has a signal state, thread 2 can get our event. That is, our two threads have entered the code to be protected, and of course the result is unpredictable.

2 in the multi-CPU platform thread 1 and 2 can run simultaneously. When they request an event object, it doesn't work if you set them to a signal state, because they're already in the code we want to protect. Two threads accessing the same resource at the same time of course the result is unknown.

*/

if (tickets>0)

{

Sleep (1);

cout<< "Thread1 Sell ticket:" <<tickets--<<endl;

}

Else

Break

SetEvent (g_hevent); Set the event object to have a signaled state

}

return 0;

}

DWORD WINAPI Fun2proc (

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.