Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ThreadSync.h
Go to the documentation of this file.
1 /* Project nGen
2  John DiCamillo
3  Copyright © 1997-2001. All Rights Reserved.
4 
5  SUBSYSTEM: foundation
6  FILE: ThreadSync.h
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Declaration of the ThreadSync class
13 */
14 
15 #ifndef ThreadSync_h
16 #define ThreadSync_h
17 
18 #include <windows.h>
19 
20 // +-------------------------------------------------------------------+
21 
23 {
24 #if defined(_MT) // MULTITHREADED: WITH SYNC ------------
25  CRITICAL_SECTION sync;
26 
27 public:
28  ThreadSync() { ::InitializeCriticalSection(&sync); }
29  ~ThreadSync() { ::DeleteCriticalSection(&sync); }
30 
31  void acquire() { ::EnterCriticalSection(&sync); }
32  void release() { ::LeaveCriticalSection(&sync); }
33 
34 #else // SINGLE THREADED: NO SYNC ------------
35 
36 public:
37  ThreadSync() { }
39 
40  void acquire() { }
41  void release() { }
42 
43 #endif
44 };
45 
46 // +-------------------------------------------------------------------+
47 
49 {
50 public:
51  AutoThreadSync(ThreadSync& s) : sync(s) { sync.acquire(); }
52  ~AutoThreadSync() { sync.release(); }
53 private:
54  ThreadSync& sync;
55 };
56 
57 #endif ThreadSync_h