2013年12月29日 星期日

delphi Critical Section、Event、Mutex、Semaphores

用於多緒TThread 等待
臨界區(Critical Section)
Txxx=class
  protected
    FSection: TRTLCriticalSection;
...
Txxx.Create;
InitializeCriticalSection(FSection); 建立臨界區

EnterCriticalSection(FSection) 進入臨界區
try
   DO...
finally
LeaveCriticalSection(FSection 離開臨界區
end;

Txxx.destory;
DeleteCriticalSection(FSection);

事件(Event)
CreateEvent() 建一個信號量
SetEvent()      on
RsetEvent(  )  off
CloseHandle()
互斥量(Mutex)
CreateMutex()
OpenMutex()
ReleaseMutex()
CloseHandle()
信號量(Semaphores)
CreateSemaphore()
OpenSemaphore()
ReleaseSemaphore()
CloseHandle()

WaitForSingleObject(等待訊號,等待時間)單一訊號
WaitForMultipleObjects(等待數量,等待訊號,等全部,等待時間)多訊號

Return code/value_
WAIT_OBJECT_0 to (WAIT_OBJECT_0 +nCount– 1)
WAIT_ABANDONED_0 to (WAIT_ABANDONED_0 +nCount– 1)
WAIT_TIMEOUT
WAIT_FAILED ...

訊號類型
Change notification
Console input
Event
Memory resource notification
Mutex
Process
Semaphore
Thread
Waitable timer

沒有留言:

張貼留言