TNB Library
TnbExchanger.h
[詳解]
1#pragma once
13#include "TnbSync.h"
14
15
16
17namespace TNB
18{
19
20
21
36template<typename TYP>
38{
39 LONG m_lCount;
40 CSyncSemaphore m_syncSema;
41 CSyncEvent m_syncEvent1;
42 CSyncEvent m_syncEvent2;
43 volatile TYP m_tParam1st;
44 volatile TYP m_tParam2nd;
45
46public:
47
50 : m_lCount(0), m_syncSema(NULL, 2), m_syncEvent1(false, true), m_syncEvent2(false, true)
51 {
52 }
53
61 TYP Exchange(const TYP& t)
62 {
63 EXCLUSIVE(&m_syncSema);
64 TYP r;
65 LONG l = ::InterlockedIncrement(&m_lCount);
66 if ( l == 1 )
67 {
68 m_tParam1st = t;
69 m_syncEvent2.Lock();
70 m_syncEvent1.Unlock();
71 r = m_tParam2nd;
72 }
73 else
74 {
75 m_lCount = 0;
76 m_tParam2nd = t;
77 m_syncEvent2.Unlock();
78 m_syncEvent1.Lock();
79 r = m_tParam1st;
80 }
81 return r;
82 }
83};
84
85
86
87}; // TNB
同期処理関係のヘッダ
virtual bool Lock(DWORD dwTime=INFINITE) const
[排他] ロック
Definition: TnbSync.h:91
スレッド間値入れ替えテンプレートクラス.
Definition: TnbExchanger.h:38
CExchangerT(void)
コンストラクタ
Definition: TnbExchanger.h:49
TYP Exchange(const TYP &t)
[取得] 値交換.
Definition: TnbExchanger.h:61
Event排他管理クラス
Definition: TnbSync.h:480
virtual void Unlock(void) const
[排他] アンロック
Definition: TnbSync.h:532
Semaphore排他管理クラス
Definition: TnbSync.h:370
#define EXCLUSIVE(CLS)
簡易排他制御マクロ.
Definition: TnbSync.h:788
TNB Library
Definition: TnbDoxyTitle.txt:2