多线程代码:vector迭代器不兼容
以下代码旨在返回尚未使用的最小非负整数。它是从多个线程调用的。如果样式看起来有点奇怪,那是因为此类旨在存在于仅包含头文件的库中。
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;
};
此代码偶尔会出现调试时问题:
vector<bool> iterators incompatible
堆栈跟踪显示 push_back()
处发生的问题 - index
为零。 STL 实现似乎认为它插入到 vector
的另一个实例的 end()
处。使用 Visual Studio 2010 Express。
有什么想法吗?这段代码是线程安全的,不是吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
get
函数当然不是线程安全的,除非您保证在创建任何线程之前调用它一次。由于您使用get
调用来获取互斥体来保护代码的其余部分,因此您肯定会得到一些意想不到的结果。std::vector
明确专门为每个 bool 使用一位,以便确实改变迭代结果(我相信它使用代理对象)。您是否尝试过使用deque
而不是vector
作为测试?The
get
function certainly isn't thread-safe unless you guarantee that it's called once before any threads are created. And since you use theget
call to get the mutex to protect the rest of the code, you could definitely get some unexpected results there.std::vector<bool>
is explicitly specialized to use one bit per bool so that does change iteration results (it uses a proxy object I believe). Have you tried usingdeque
instead ofvector
just as a test?