Advertisement
Guest User

Untitled

a guest
Nov 26th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. struct Lock //thanks to 'doublep' from StackOverflow for this RAII solution, edited it
  2. {
  3. MUTEX mutex;
  4. bool locked;
  5.  
  6. Lock (MUTEX mutex)
  7. : mutex (mutex),
  8. locked (false)
  9. {
  10. }
  11.  
  12. ~Lock ()
  13. {
  14. release ();
  15. }
  16.  
  17. bool acquire (int timeout = -1)
  18. {
  19.  
  20. if (!locked && TryEnterCriticalSection(&mutex) != 0)
  21. {
  22. locked = true;
  23. }
  24. return locked;
  25. }
  26.  
  27. int release ()
  28. {
  29. if (locked)
  30. {
  31. LeaveCriticalSection(&mutex); locked = false;
  32. return true;
  33. }
  34. return false;
  35. }
  36. };
  37.  
  38. MUTEX mutex_q;
  39. MUTEX mutex_p;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement