Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Use Mutex instead of Critical Section. There is a little difference between mutexes and critical sections - critical sections are more effective while mutexes are more flexible. Your can easily switch between mutexes and critical sections, using for example mutexes in debug version.
- for critical section we use:
- var
- FLock: TRTLCriticalSection;
- InitializeCriticalSection(FLock); // create lock
- DeleteCriticalSection(FLock); // free lock
- EnterCriticalSection(FLock); // acquire lock
- LeaveCriticalSection(FLock); // release lock
- var FLock: THandle;
- FLock:= CreateMutex(nil, False, nil); // create lock
- CloseHandle(FLock); // free lock
- WaitForSingleObject(FLock, Timeout); // acquire lock
- ReleaseMutex(FLock); // release lock
- function AcquireLock(Lock: THandle; TimeOut: LongWord): Boolean;
- begin
- Result:= WaitForSingleObject(Lock, Timeout) = WAIT_OBJECT_0;
- end;
- while not TryEnterCriticalSection(fLock) and (additional_checks) do
- begin
- deal_with_failure();
- sleep(500); // wait 500 ms
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement