summaryrefslogtreecommitdiff
path: root/src/itocounter.h
blob: afca8dad6d9130064a911a0476ebe967647e4c72 (plain)
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
/*
 * Copyright (C) 2007-2008 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#ifndef BU_ITO_COUNTER_H
#define BU_ITO_COUNTER_H

#include "itomutex.h"

namespace Bu
{
	/**
	 * A simple thread-safe counter class.  This is handy for assigning unique
	 * IDs to objects that are being created in different threads.
	 *@ingroup Threading Containers
	 */
	template <class T>
	class ItoCounter
	{
	public:
		ItoCounter() :
			tCounter( 0 )
		{
		}
		
		virtual ~ItoCounter()
		{
		}

		T next()
		{
			mOperate.lock();
			T tRet = tCounter;
			tCounter++;
			mOperate.unlock();

			return tRet;
		}

	private:
		T tCounter;				/**< The counter itself. */
		ItoMutex mOperate;	/**< The master mutex, used on all operations. */
	};
}

#endif