我有一些静态用户数据,如:
private:
static std::map<unsigned long, UserDataSharedPtr> userStore_;
static boost::mutex mutexUserData;
public:
static void RemoveUserData(unsigned long id)
{
boost::lock_guard<boost::mutex> lock(mutexUserData);
std::map<unsigned long, UserDataSharedPtr>::iterator it = userStore_.find(id);
if (it != userStore_.end())
{
userStore_.erase(it);
}
}
static void AddUserData(unsigned long id, UserDataSharedPtr ud)
{
boost::lock_guard<boost::mutex> lock(mutexUserData);
userStore_.insert(std::make_pair(id, ud));
}
在负载测试中,我的程序崩溃了:
boost::lock_guard<boost::mutex> lock(mutexUserData);
例外:
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::lock_error> >'
what(): boost::lock_error
调用堆栈:
boost::mutex::lock() at mutex.hpp:55 0x81aeb22
boost::lock_guard<boost::mutex>::lock_guard() at locks.hpp:257 0x81b2cb3
..........RemoveUserData() at ..............:69 0x81b0b28
mutex.hpp上的boost::mutex::lock()
pthread_mutex_t m;
void lock()
{
int const res=pthread_mutex_lock(&m);
if(res)
{
boost::throw_exception(lock_error(res));
}
}
此处pthread_mutex_lock(&m)
返回22,我检查22是EINVAL
:The mutex was created with the protocol attribute having the value PTHREAD_PRIO_PROTECT and the calling thread's priority is higher than the mutex's current priority ceiling
我该怎么办?
我搜索了很多,但我没有运气。
感谢。
彼得
答案 0 :(得分:3)
实际上,错误更可能是这个错误:
如果出现以下情况,pthread_mutex_lock(),pthread_mutex_trylock()和pthread_mutex_unlock()函数可能会失败:
[EINVAL]
mutex指定的值不是指初始化的互斥对象。
这通常是由内存损坏引起的。您可以尝试在valgrind
或类似工具下运行。
答案 1 :(得分:0)
我冒昧地猜测,当从不同的编译单元调用AddUserData
或RemoveUserData
时,您正在遇到静态问题的初始化顺序。我在审核单身实施here, with a link to a workaround时遇到的问题,也讨论了here。