首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pthread_mutex_lock __pthread_mutex_lock_full:断言失败,错误为robust和0x4000000

pthread_mutex_lock __pthread_mutex_lock_full:断言失败,错误为robust和0x4000000
EN

Stack Overflow用户
提问于 2012-03-30 08:52:54
回答 1查看 15.8K关注 0票数 3

我正在做一个服务器端项目,它应该可以接受100多个客户端连接。

它是使用boost::thread的多线程程序。在某些地方,我使用boost::lock_guard<boost::mutex>来锁定共享成员数据。还有一个包含输入连接的BlockingQueue<ConnectionPtr>BlockingQueue的实现

代码语言:javascript
复制
template <typename DataType>
class BlockingQueue : private boost::noncopyable
{
public:
    BlockingQueue()
        : nblocked(0), stopped(false)
    {

    }

    ~BlockingQueue()
    {
        Stop(true);
    }

    void Push(const DataType& item)
    {
        boost::mutex::scoped_lock lock(mutex);
        queue.push(item);
        lock.unlock();
        cond.notify_one(); // cond.notify_all();
    }

    bool Empty() const
    {
        boost::mutex::scoped_lock lock(mutex);
        return queue.empty();
    }

    std::size_t Count() const
    {
        boost::mutex::scoped_lock lock(mutex);
        return queue.size();
    }

    bool TryPop(DataType& poppedItem)
    {
        boost::mutex::scoped_lock lock(mutex);
        if (queue.empty())
            return false;

        poppedItem = queue.front();
        queue.pop();

        return true;
    }

    DataType WaitPop()
    {
        boost::mutex::scoped_lock lock(mutex);

        ++nblocked;
        while (!stopped && queue.empty()) // Or: if (queue.empty())
            cond.wait(lock);
        --nblocked;

        if (stopped)
        {
            cond.notify_all(); // Tell Stop() that this thread has left
            BOOST_THROW_EXCEPTION(BlockingQueueTerminatedException());
        }

        DataType tmp(queue.front());
        queue.pop();

        return tmp;
    }

    void Stop(bool wait)
    {
        boost::mutex::scoped_lock lock(mutex);
        stopped = true;
        cond.notify_all();

        if (wait) // Wait till all blocked threads on the waiting queue to leave BlockingQueue::WaitPop()
        {
            while (nblocked)
                cond.wait(lock);
        }
    }

private:
    std::queue<DataType>          queue;
    mutable boost::mutex          mutex;
    boost::condition_variable_any cond;
    unsigned int                  nblocked;
    bool                          stopped;
};

对于每个Connection,都有一个包含输入流的ConcurrentQueue<StreamPtr>ConcurrentQueue的实现

代码语言:javascript
复制
template <typename DataType>
class ConcurrentQueue : private boost::noncopyable
{
public:
    void Push(const DataType& item)
    {
        boost::mutex::scoped_lock lock(mutex);
        queue.push(item);
    }

    bool Empty() const
    {
        boost::mutex::scoped_lock lock(mutex);
        return queue.empty();
    }

    bool TryPop(DataType& poppedItem)
    {
        boost::mutex::scoped_lock lock(mutex);
        if (queue.empty())
            return false;

        poppedItem = queue.front();
        queue.pop();

        return true;
    }
private:
    std::queue<DataType> queue;
    mutable boost::mutex mutex;
};

当调试程序时,这是可以的。但在具有50个或100个或更多客户端连接的负载测试中,有时会出现以下错误

代码语言:javascript
复制
pthread_mutex_lock.c:321: __pthread_mutex_lock_full: Assertion `robust || (oldval & 0x40000000) == 0' failed.

我不知道发生了什么,而且它不可能每次都被重现。

我用谷歌搜索了很多,但没找到。请给我建议。

谢谢。

彼得

EN

回答 1

Stack Overflow用户

发布于 2012-07-26 02:09:46

0x40000000FUTEX_OWNER_DIED -它的futex.h头中包含以下文档:

代码语言:javascript
复制
/*
 * The kernel signals via this bit that a thread holding a futex
 * has exited without unlocking the futex. The kernel also does
 * a FUTEX_WAKE on such futexes, after setting the bit, to wake
 * up any possible waiters:
 */
#define FUTEX_OWNER_DIED        0x40000000

因此,断言似乎表明持有锁的线程出于某种原因正在退出-有没有一种方法可以在持有锁的同时销毁线程对象?

另一件要检查的事情是,你是否有某种类型的内存损坏。Valgrind可能是一个可以帮助你做到这一点的工具。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9935665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档