ACT-CV - Machine Vision for Cognitive Modeling
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
general
Threads
ThreadSafeVector.h
Go to the documentation of this file.
1
// -*- mode: c++; indent-tabs-mode: nil; c-basic-offset: 4; coding: iso-8859-1; -*-
2
3
/*
4
ACT-CV - Machine Vision for Cognitive Modeling
5
Copyright (c) 2008 Marc Halbruegge
6
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
11
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
*/
21
27
#ifndef __THREADSAFEVECTOR_H
28
#define __THREADSAFEVECTOR_H
29
30
namespace
mhthreads {
31
32
#include <vector>
33
#include <windows.h>
34
#include <process.h>
35
#include <algorithm>
36
#include <functional>
37
39
template
<
class
_t>
40
class
ThreadSafeVector
:
public
std::vector<_t> {
41
CRITICAL_SECTION
protectMe
;
42
public
:
43
ThreadSafeVector
() {
44
InitializeCriticalSection(&
protectMe
);
45
}
46
ThreadSafeVector
(
const
ThreadSafeVector
&v) :
47
std::vector<_t>(v) {
48
InitializeCriticalSection(&
protectMe
);
49
}
50
~ThreadSafeVector
() {
51
DeleteCriticalSection(&
protectMe
);
52
}
53
void
push_back
(
const
_t& x) {
54
::EnterCriticalSection
(&
protectMe
);
55
std::vector<_t>::push_back(x);
56
::LeaveCriticalSection
(&
protectMe
);
57
}
58
void
remove
(
const
_t& x) {
59
::EnterCriticalSection
(&
protectMe
);
60
std::vector<_t>::iterator newEnd;
61
newEnd = std::remove_if(begin(),end(),std::bind2nd(std::equal_to<_t>(),x));
62
erase(newEnd,end());
63
::LeaveCriticalSection
(&
protectMe
);
64
}
65
void
EnterCriticalSection
() {
66
::EnterCriticalSection
(&
protectMe
);
67
}
68
void
LeaveCriticalSection
() {
69
::LeaveCriticalSection
(&
protectMe
);
70
}
71
};
72
73
};
74
75
#endif
76
ACT-CV - Machine Vision for Cognitive Modeling
© 2015
Marc Halbruegge
(actcvlibrary@googlemail.com)