-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSync.h
64 lines (49 loc) · 1.8 KB
/
Sync.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#ifndef SYNC_H
#define SYNC_H
#include <windows.h>
#include <assert.h>
//-------------------------------------------
// イベントさん
class Event {
HANDLE mEvent;
Event(const Event&);
operator=(const Event&);
public:
Event( BOOL iInitialState=FALSE, BOOL iManualReset=TRUE ) {
mEvent = ::CreateEvent(NULL, iInitialState, iManualReset, NULL);
}
~Event() { ::CloseHandle(mEvent); }
void Set() { ::SetEvent(mEvent); }
void Reset() { ::ResetEvent(mEvent); }
BOOL isSet() { return ( ::WaitForSingleObject(mEvent, 0)==WAIT_OBJECT_0 ); }
BOOL WaitSet(DWORD iTimeOut=INFINITE) { return ( ::WaitForSingleObject(mEvent, iTimeOut)==WAIT_OBJECT_0 ); }
operator HANDLE() { return mEvent; }
};
//-------------------------------------------
// 2つの同期オブジェクトの片方がシグナル状態になったら返る
inline bool WaitFor2(HANDLE iObj1, HANDLE iObj2, DWORD iTimeOut=INFINITE) {
HANDLE theHandles[2] = { iObj1, iObj2 };
return (::WaitForMultipleObjects(2, theHandles, TRUE, iTimeOut) != WAIT_TIMEOUT);
}
inline bool WaitFor1of2(HANDLE iObj1, HANDLE iObj2, DWORD iTimeOut=INFINITE) {
HANDLE theHandles[2] = { iObj1, iObj2 };
return (::WaitForMultipleObjects(2, theHandles, FALSE, iTimeOut) != WAIT_TIMEOUT);
}
//-------------------------------------------
// クリティカルセクションさん
#include "CriticalSection.h"
//-------------------------------------------
// 非同期をサポートしたフラグ
class async_flag {
volatile bool mFlag;
public:
async_flag(bool iFlag=false) : mFlag(iFlag) {}
~async_flag() {}
void set(bool iFlag=true) { mFlag=iFlag; }
void reset() { mFlag=false; }
bool get() const { return mFlag; }
operator bool() const { return mFlag; }
operator !() const { return !mFlag; }
operator =(bool iFlag) { mFlag=iFlag; return *this; }
};
#endif // SYNC_H