Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. 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.
  2.  
  3. for critical section we use:
  4.  
  5. var
  6. FLock: TRTLCriticalSection;
  7.  
  8. InitializeCriticalSection(FLock); // create lock
  9. DeleteCriticalSection(FLock); // free lock
  10. EnterCriticalSection(FLock); // acquire lock
  11. LeaveCriticalSection(FLock); // release lock
  12.  
  13. var FLock: THandle;
  14.  
  15. FLock:= CreateMutex(nil, False, nil); // create lock
  16. CloseHandle(FLock); // free lock
  17. WaitForSingleObject(FLock, Timeout); // acquire lock
  18. ReleaseMutex(FLock); // release lock
  19.  
  20. function AcquireLock(Lock: THandle; TimeOut: LongWord): Boolean;
  21. begin
  22. Result:= WaitForSingleObject(Lock, Timeout) = WAIT_OBJECT_0;
  23. end;
  24.  
  25. while not TryEnterCriticalSection(fLock) and (additional_checks) do
  26. begin
  27. deal_with_failure();
  28. sleep(500); // wait 500 ms
  29. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement