C++ 事件对象 多线程同步互斥
2016-05-17 11:13
399 查看
事件对象包含的几个操作原语:
CreateEvent() 创建一个信号量
OpenEvent() 打开一个事件
SetEvent() 回置事件
WaitForSingleObject() 等待一个事件
CreateEvent() 创建一个信号量
OpenEvent() 打开一个事件
SetEvent() 回置事件
WaitForSingleObject() 等待一个事件
#include <stdio.h> #define THREADCOUNT 4 HANDLE ghWriteEvent; HANDLE ghThreads[THREADCOUNT]; DWORD WINAPI ThreadProc(LPVOID); void CreateEventsAndThreads(void) { int i; DWORD dwThreadID; // Create a manual-reset event object. The write thread sets this // object to the signaled state when it finishes writing to a // shared buffer. ghWriteEvent = CreateEvent( NULL, // default security attributes TRUE, // manual-reset event FALSE, // initial state is nonsignaled TEXT("WriteEvent") // object name ); if (ghWriteEvent == NULL) { printf("CreateEvent failed (%d)\n", GetLastError()); return; } // Create multiple threads to read from the buffer. for(i = 0; i < THREADCOUNT; i++) { // TODO: More complex scenarios may require use of a parameter // to the thread procedure, such as an event per thread to // be used for synchronization. ghThreads[i] = CreateThread( NULL, // default security 0, // default stack size ThreadProc, // name of the thread function NULL, // no thread parameters 0, // default startup flags &dwThreadID); if (ghThreads[i] == NULL) { printf("CreateThread failed (%d)\n", GetLastError()); return; } } } void WriteToBuffer(VOID) { // TODO: Write to the shared buffer. printf("Main thread writing to the shared buffer...\n"); // Set ghWriteEvent to signaled if (! SetEvent(ghWriteEvent) ) { printf("SetEvent failed (%d)\n", GetLastError()); return; } } void CloseEvents() { // Close all event handles (currently, only one global handle). CloseHandle(ghWriteEvent); } int main( void ) { DWORD dwWaitResult; // TODO: Create the shared buffer // Create events and THREADCOUNT threads to read from the buffer CreateEventsAndThreads(); // At this point, the reader threads have started and are most // likely waiting for the global event to be signaled. However, // it is safe to write to the buffer because the event is a // manual-reset event. WriteToBuffer(); printf("Main thread waiting for threads to exit...\n"); // The handle for each thread is signaled when the thread is // terminated. dwWaitResult = WaitForMultipleObjects( THREADCOUNT, // number of handles in array ghThreads, // array of thread handles TRUE, // wait until all are signaled INFINITE); switch (dwWaitResult) { // All thread objects were signaled case WAIT_OBJECT_0: printf("All threads ended, cleaning up for application exit...\n"); break; // An error occurred default: printf("WaitForMultipleObjects failed (%d)\n", GetLastError()); return 1; } // Close the events to clean up CloseEvents(); system("pause"); return 0; } DWORD WINAPI ThreadProc(LPVOID lpParam) { // lpParam not used in this example. UNREFERENCED_PARAMETER(lpParam); DWORD dwWaitResult; printf("Thread %d waiting for write event...\n", GetCurrentThreadId()); dwWaitResult = WaitForSingleObject( ghWriteEvent, // event handle INFINITE); // indefinite wait switch (dwWaitResult) { // Event object was signaled case WAIT_OBJECT_0: // // TODO: Read from the shared buffer // printf("Thread %d reading from buffer\n", GetCurrentThreadId()); break; // An error occurred default: printf("Wait error (%d)\n", GetLastError()); return 0; } // Now that we are done reading the buffer, we could use another // event to signal that this thread is no longer reading. This // example simply uses the thread handle for synchronization (the // handle is signaled when the thread terminates.) printf("Thread %d exiting\n", GetCurrentThreadId()); return 1; }
相关文章推荐
- 深入理解c++常成员函数和常对象
- C++ 信号量 多线程同步互斥
- 00_ubuntu下vim的c++补全
- Geekband C++面向对象高级编程(上) 第二周笔记 暗影行者
- 巧妙程序写法
- c/c++ 结束进程
- C++ local static对象和non-local static对象在初始化时机上的差异
- C语言练习题:自由落体的小球简单实例
- 新增的C语言完整源代码
- C语言练习题:求1到10的阶乘之和简单实例
- OpenCV中C++函数imread读取图片的缩放问题
- C++程序设计必知:常引用、常对象和对象的常成员
- C / C++ 输入输出缓存处理
- C语言指针变量可以当做数组运用
- 2016年,C语言该怎样写
- gsoap笔记 C++
- 330. Patching Array (C++实现)
- 深入理解C++中public、protected及private用法
- OC语言笔记(十二):OC字符串、数组、字典常用方法大全
- OC语言笔记(十一):OC沙盒查找步骤