建议你装一套msdn,作为一个程序员没有msdn是不行的。<br>给你发一段代码,从msdn上copy的<br>这段代码写的就是如何用TEven内核对象来同步程序执行的<br>---------------------------------------------------<br> <br>Platform SDK: DLLs, Processes, and Threads <br>Using Event Objects<br>Win32-based applications use event objects in a number of situations to notify a waiting thread of the occurrence of an event. For example, overlapped I/O operations on files, named pipes, and communications devices use an event object to signal their completion. For more information about the use of event objects in overlapped I/O operations, see Synchronization and Overlapped Input and Output.<br><br>In the following example, an application uses event objects to prevent several threads from reading from a shared memory buffer while a master thread is writing to that buffer. First, the master thread uses the CreateEvent function to create a manual-reset event object. The master thread sets the event object to nonsignaled when it is writing to the buffer and then resets the object to signaled when it has finished writing. Then it creates several reader threads and an auto-reset event object for each thread. Each reader thread sets its event object to signaled when it is not reading from the buffer. <br><br>#define NUMTHREADS 4 <br><br>HANDLE hGlobalWriteEvent; <br><br>void CreateEventsAndThreads(void) <br>{<br> HANDLE hReadEvents[NUMTHREADS], hThread; <br> DWORD i, IDThread; <br><br> // Create a manual-reset event object. The master thread sets <br> // this to nonsignaled when it writes to the shared buffer. <br><br> hGlobalWriteEvent = CreateEvent( <br> NULL, // no security attributes<br> TRUE, // manual-reset event<br> TRUE, // initial state is signaled<br> "WriteEvent" // object name<br> ); <br><br> if (hGlobalWriteEvent == NULL) { <br> // error exit<br> }<br><br> // Create multiple threads and an auto-reset event object <br> // for each thread. Each thread sets its event object to <br> // signaled when it is not reading from the shared buffer. <br><br> for(i = 1; i <= NUMTHREADS; i++) <br> {<br> // Create the auto-reset event.<br> hReadEvents = CreateEvent( <br> NULL, // no security attributes<br> FALSE, // auto-reset event<br> TRUE, // initial state is signaled<br> NULL); // object not named<br><br> if (hReadEvents == NULL) <br> {<br> // Error exit.<br> }<br><br> hThread = CreateThread(NULL, 0, <br> (LPTHREAD_START_ROUTINE) ThreadFunction, <br> &hReadEvents, // pass event handle<br> 0, &IDThread); <br> if (hThread == NULL) <br> {<br> // Error exit.<br> }<br> }<br>}<br>Before the master thread writes to the shared buffer, it uses the ResetEvent function to set the state of hGlobalWriteEvent (an application-defined global variable) to nonsignaled. This blocks the reader threads from starting a read operation. The master then uses the WaitForMultipleObjects function to wait for all reader threads to finish any current read operations. When WaitForMultipleObjects returns, the master thread can safely write to the buffer. After it has finished, it sets hGlobalWriteEvent and all the reader-thread events to signaled, enabling the reader threads to resume their read operations. <br><br>VOID WriteToBuffer(VOID) <br>{<br> DWORD dwWaitResult, i; <br><br> // Reset hGlobalWriteEvent to nonsignaled, to block readers.<br> <br> if (! ResetEvent(hGlobalWriteEvent) ) <br> { <br> // Error exit.<br> } <br><br> // Wait for all reading threads to finish reading.<br><br> dwWaitResult = WaitForMultipleObjects( <br> NUMTHREADS, // number of handles in array<br> hReadEvents, // array of read-event handles<br> TRUE, // wait until all are signaled<br> INFINITE); // indefinite wait<br><br> switch (dwWaitResult) <br> {<br> // All read-event objects were signaled.<br> case WAIT_OBJECT_0: <br> // Write to the shared buffer.<br> break;<br><br> // An error occurred.<br> default: <br> printf("Wait error: %d/n", GetLastError()); <br> ExitProcess(0); <br> } <br><br> // Set hGlobalWriteEvent to signaled.<br><br> if (! SetEvent(hGlobalWriteEvent) ) <br> {<br> // Error exit.<br> }<br><br> // Set all read events to signaled.<br> for(i = 1; i <= NUMTHREADS; i++) <br> if (! SetEvent(hReadEvents) ) { <br> // Error exit.<br> } <br>}<br>Before starting a read operation, each reader thread uses WaitForMultipleObjects to wait for the application-defined global variable hGlobalWriteEvent and its own read event to be signaled. When WaitForMultipleObjects returns, the reader thread's auto-reset event has been reset to nonsignaled. This blocks the master thread from writing to the buffer until the reader thread uses the SetEvent function to set the event's state back to signaled. <br><br>VOID ThreadFunction(LPVOID lpParam) <br>{<br> DWORD dwWaitResult;<br> HANDLE hEvents[2]; <br><br> hEvents[0] = *(HANDLE*)lpParam; // thread's read event<br> hEvents[1] = hGlobalWriteEvent; <br><br> dwWaitResult = WaitForMultipleObjects( <br> 2, // number of handles in array<br> hEvents, // array of event handles<br> TRUE, // wait till all are signaled<br> INFINITE); // indefinite wait<br><br> switch (dwWaitResult) <br> {<br><br> // Both event objects were signaled.<br> case WAIT_OBJECT_0: <br> // Read from the shared buffer.<br> break; <br><br> // An error occurred.<br> default: <br> printf("Wait error: %d/n", GetLastError()); <br> ExitThread(0); <br> }<br><br> // Set the read event to signaled.<br><br> if (! SetEvent(hEvents[0]) ) <br> { <br> // Error exit.<br> } <br>}<br>Built on Thursday, May 11, 2000<br>----------------------------------------------------<br>