閱讀941 返回首頁    go 技術社區[雲棲]


線程同步之EVENT

事件可傳信給其他線程,表示某些條件現在已具備,比如有可用的消息。

事件可分為手動複位和自動複位,前者可傳信給許多同時等待事件的線程而且可以被複位。

自動複位的事件傳信給單個等待時間的線程,該事件會自動複位。

Applications can 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. 

The following example(From MSDN)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 whose initial state is nonsignaled. Then it creates several reader threads. The master thread performs a write operation and then sets the event object to the signaled state when it has finished writing.

Before starting a read operation, each reader thread uses WaitForSingleObject to wait for the manual-reset event object to be signaled. When WaitForSingleObject returns, this indicates that the main thread is ready for it to begin its read operation.



最後更新:2017-04-03 05:39:54

  上一篇:go java對象和json數據轉換實現方式3-使用jackson實現
  下一篇:go eclipse GBK轉UTF-8