-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTSArray.h
63 lines (51 loc) · 934 Bytes
/
TSArray.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
// A thread safe granular-locking array by Tim Bashford, 10/05/13
#pragma once
#include <set>
#include <boost\thread.hpp>
template<typename T>
class TSArray
{
public:
TSArray(unsigned s);
~TSArray(void);
T& operator[](unsigned i);
void Lock(unsigned ind);
void Unlock(unsigned ind);
private:
T* d;
std::set<T*> lv;
boost::mutex mut;
boost::condition_variable cond;
};
template<typename T>
TSArray<T>::TSArray(unsigned s)
{
d = new T[s];
}
template<typename T>
TSArray<T>::~TSArray(void)
{
delete d;
}
template<typename T>
T& TSArray<T>::operator[](unsigned i)
{
return d[i];
}
template<typename T>
void TSArray<T>::Lock(unsigned ind)
{
boost::unique_lock<boost::mutex> l(mut);
while(lv.find(&(d[ind])) != lv.end())
{
cond.wait(l);
}
lv.insert(&(d[ind]));
}
template<typename T>
void TSArray<T>::Unlock(unsigned ind)
{
boost::lock_guard<boost::mutex> l(mut);
lv.erase(&(d[ind]));
cond.notify_all();
}