2011-10-04 92 views
2

我有一些静态的用户数据,如:升压lock_guard <提高::互斥>抛出异常EINVAL

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 

而且boost::mutex::lock()在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 EINVALThe 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

我该怎么办?

我搜索了很多,但我没有运气。

谢谢。

彼得

回答

3

其实,错误更可能是这一个:

的pthread_mutex_lock()的,pthread_mutex_trylock()和调用pthread_mutex_unlock()函数可能会失败,如果:

[ EINVAL]
互斥量指定的值不引用初始化的互斥对象。

这通常是由内存损坏引起的。您可以尝试在valgrind或类似工具下运行。

+0

嗯,我从EINVAL的解释:http://linux.die.net/man/3/pthread_mutex_lock –

+0

异常只能在我进行负载测试时抛出。如果我使用Valgrind的,我怀疑这会是超级慢,Valgrind的肯定会影响程序本身。但我会尝试。 –

+0

@Peter李:读那一页上两行。 –

0

我想冒险猜测你遇到了静态问题的初始化顺序,从AddUserDataRemoveUserData调用不同的编译单元。我跑审查单身实现here, with a link to a workaround时进入的问题,还讨论here