diff options
Diffstat (limited to '')
-rw-r--r-- | src/itocounter.cpp | 1 | ||||
-rw-r--r-- | src/itocounter.h | 50 |
2 files changed, 51 insertions, 0 deletions
diff --git a/src/itocounter.cpp b/src/itocounter.cpp new file mode 100644 index 0000000..35298a9 --- /dev/null +++ b/src/itocounter.cpp | |||
@@ -0,0 +1 @@ | |||
#include "bu/itocounter.h" | |||
diff --git a/src/itocounter.h b/src/itocounter.h new file mode 100644 index 0000000..58abe07 --- /dev/null +++ b/src/itocounter.h | |||
@@ -0,0 +1,50 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2008 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #ifndef BU_ITO_COUNTER_H | ||
9 | #define BU_ITO_COUNTER_H | ||
10 | |||
11 | #include "itomutex.h" | ||
12 | |||
13 | namespace Bu | ||
14 | { | ||
15 | /** | ||
16 | * A simple thread-safe counter class. This is handy for assigning unique | ||
17 | * IDs to objects that are being created in different threads. | ||
18 | *@author Mike Buland | ||
19 | *@ingroup Threading Containers | ||
20 | */ | ||
21 | template <class T> | ||
22 | class ItoCounter | ||
23 | { | ||
24 | public: | ||
25 | ItoCounter() : | ||
26 | tCounter( 0 ) | ||
27 | { | ||
28 | } | ||
29 | |||
30 | virtual ~ItoCounter() | ||
31 | { | ||
32 | } | ||
33 | |||
34 | T next() | ||
35 | { | ||
36 | mOperate.lock(); | ||
37 | T tRet = tCounter; | ||
38 | tCounter++; | ||
39 | mOperate.unlock(); | ||
40 | |||
41 | return tRet; | ||
42 | } | ||
43 | |||
44 | private: | ||
45 | T tCounter; /**< The counter itself. */ | ||
46 | ItoMutex mOperate; /**< The master mutex, used on all operations. */ | ||
47 | }; | ||
48 | } | ||
49 | |||
50 | #endif | ||