多线程代码:vector迭代器不兼容

发布于 2024-10-31 14:23:40 字数 1144 浏览 1 评论 0原文

以下代码旨在返回尚未使用的最小非负整数。它是从多个线程调用的。如果样式看起来有点奇怪,那是因为此类旨在存在于仅包含头文件的库中。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我是有多爱你 2024-11-07 14:23:40

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 the get 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 using deque instead of vector just as a test?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文