From ee8174b0dd72204a38a3818686623edd87d5e10e Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 26 Feb 2008 06:53:45 +0000 Subject: 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! --- src/itocounter.cpp | 1 + src/itocounter.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/itocounter.cpp create mode 100644 src/itocounter.h (limited to 'src') 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 @@ +/* + * 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. + *@author Mike Buland + *@ingroup Threading Containers + */ + template + 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 -- cgit v1.2.3