diff options
author | Mike Buland <eichlan@xagasoft.com> | 2008-02-26 06:53:45 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2008-02-26 06:53:45 +0000 |
commit | ee8174b0dd72204a38a3818686623edd87d5e10e (patch) | |
tree | deb4ca4e992cf5f41b0f506d092ba86bb80157b2 | |
parent | 3cea09281c9f809fc78de6f2e79f4117976f74e4 (diff) | |
download | libbu++-ee8174b0dd72204a38a3818686623edd87d5e10e.tar.gz libbu++-ee8174b0dd72204a38a3818686623edd87d5e10e.tar.bz2 libbu++-ee8174b0dd72204a38a3818686623edd87d5e10e.tar.xz libbu++-ee8174b0dd72204a38a3818686623edd87d5e10e.zip |
Thought this may be handy. It should have more functions and things later, but
it's basically an uber-simple counter class that's thread-safe!
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 | ||