Pet*_*Lee 3 c++ assert mutex pthreads
我正在开发一个服务器端项目,该项目应该接受100多个客户端连接.
它是使用boost :: thread的多线程程序.我boost::lock_guard<boost::mutex>用来锁定共享成员数据的一些地方.还有一个BlockingQueue<ConnectionPtr>包含输入连接.执行BlockingQueue:
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;
};
Run Code Online (Sandbox Code Playgroud)
对于每个Connection,都有一个ConcurrentQueue<StreamPtr>包含输入Streams的.执行ConcurrentQueue:
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;
};
Run Code Online (Sandbox Code Playgroud)
调试程序时,没关系.但是在使用50或100个或更多客户端连接进行负载测试时,有时会中止
pthread_mutex_lock.c:321: __pthread_mutex_lock_full: Assertion `robust || (oldval & 0x40000000) == 0' failed.
Run Code Online (Sandbox Code Playgroud)
我不知道发生了什么,每次都无法复制.
我google了很多,但没有运气.请指教.
谢谢.
彼得
0x40000000是FUTEX_OWNER_DIED- futex.h标题中包含以下文档:
/*
* 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
Run Code Online (Sandbox Code Playgroud)
因此断言似乎表明持有锁的线程由于某种原因而退出 - 是否有一种方法可以在线程对象持有锁时被销毁?
要检查的另一件事是你是否在某处有某种内存损坏.Valgrind可能是一个可以帮助你的工具.