Multithreaded code: vector<bool> iterators incompatible
The following code is intended to return the smallest non-negative integer that is not already in use.  It is called from multiple threads.  If style looks a bit odd, it's because this class is intended to live in a header-only library.

	class Unique
	{
	public:
		static unsigned int getIndex()
		{
			boost::unique_lock<boost::mutex> lock(get().mutex);
			unsigned int index = 0;
			while (index < get().valueInUse.size() && get().valueInUse[index])
				index++;
			if (index == get().valueInUse.size()) get().valueInUse.push_back(true);
			get().valueInUse[index] = true;
			return index;
		}
		static void releaseIndex(unsigned int index)
		{
			boost::unique_lock<boost::mutex> lock(get().mutex);
			get().valueInUse[index] = false;
		}
	private:
		static Unique &get()
		{
			static Unique s;
			return s;
		}
		boost::mutex mutex;
		std::vector<bool> valueInUse;
	};

A debug-time problem occasionally occurs with this code:

    vector<bool> iterators incompatible

Stack trace shows the problem happening at the `push_back()` - `index` is zero.  The STL implementation seems to think it is inserting at the `end()` of another instance of `vector<bool>`.  Using Visual Studio 2010 Express.

Any ideas?  This code is thread-safe, isn't it?
Multithreaded code: vector<bool> iterators incompatible
===

The following code is intended to return the smallest non-negative integer that is not already in use.  It is called from multiple threads.  If style looks a bit odd, it's because this class is intended to live in a header-only library.

	class Unique
	{
	public:
		static unsigned int getIndex()
		{
			boost::unique_lock<boost::mutex> lock(get().mutex);
			unsigned int index = 0;
			while (index < get().valueInUse.size() && get().valueInUse[index])
				index++;
			if (index == get().valueInUse.size()) get().valueInUse.push_back(true);
			get().valueInUse[index] = true;
			return index;
		}
		static void releaseIndex(unsigned int index)
		{
			boost::unique_lock<boost::mutex> lock(get().mutex);
			get().valueInUse[index] = false;
		}
	private:
		static Unique &get()
		{
			static Unique s;
			return s;
		}
		boost::mutex mutex;
		std::vector<bool> valueInUse;
	};

A debug-time problem occasionally occurs with this code:

    vector<bool> iterators incompatible

Stack trace shows the problem happening at the `push_back()` - `index` is zero.  The STL implementation seems to think it is inserting at the `end()` of another instance of `vector<bool>`.  Using Visual Studio 2010 Express.

Any ideas?  This code is thread-safe, isn't it?