Boost lock_guard< boost :: mutex>抛出EINVAL异常 [英] Boost lock_guard<boost::mutex> Throws EINVAL Exception
问题描述
我有一些静态用户数据,如:
I have some static user data like:
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));
}
在加载测试中,我的程序崩溃:
And in a load testing, my program crashes at the line:
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
调用堆栈: p>
The Call Stack:
boost::mutex::lock() at mutex.hpp:55 0x81aeb22
boost::lock_guard<boost::mutex>::lock_guard() at locks.hpp:257 0x81b2cb3
..........RemoveUserData() at ..............:69 0x81b0b28
而$ code boost :: mutex :: lock()在mutex.hpp
And the boost::mutex::lock()
at mutex.hpp
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
:互斥体是使用协议属性创建的,值为PTHREAD_PRIO_PROTECT并且调用线程的优先级高于互斥量当前的优先级上限
Here pthread_mutex_lock(&m)
returns 22, and I check 22 is 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
我该怎么办?
我已经很多,但我没有运气。
I googled a lot, but I got no luck.
谢谢。
彼得
推荐答案
实际上,错误更可能是这样的:
Actually, the error is much more likely to be this one:
pthread_mutex_lock(),pthread_mutex_trylock()和pthread_mutex_unlock()函数可能会失败,如果:
The pthread_mutex_lock(), pthread_mutex_trylock() and pthread_mutex_unlock() functions may fail if:
[EINVAL]
mutex指定的值不是指初始化的互斥对象。
[EINVAL]
The value specified by mutex does not refer to an initialised mutex object.
这通常是由内存损坏引起的。您可以尝试运行 valgrind
或类似的工具。
This is usually caused by memory corruption. You can try running under valgrind
or a similar tool.
这篇关于Boost lock_guard< boost :: mutex>抛出EINVAL异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!