2
目前在我的代码我有段像这样有什么比这个代码的boost mutex更快?
boost::mutex Mymutex
void methodA()
{
boost::mutex::scoped_lock lock(Mymutex);
......
......
......
}
,我读了关键的部分是不是互斥快?所以我正在做这样的事情,我想知道这是否更快。
boost::recursive_mutex m_guard;
void methodA()
{
// this section is not locked
{
boost::lock_guard<boost::recursive_mutex> lock(m_guard);
// this section is locked
}
//This section not locked
}
第二种方法更快吗?我主要使用互斥锁的原因是为了防止竞争条件并锁定对方法的访问,以便一次线程访问它。有什么比这更快的吗?我的其他关注是在声明
boost::lock_guard<boost::recursive_mutex> lock(m_guard);
似乎每次methodA()将被称为锁将创建在堆栈上。 我正在考虑将锁声明为静态变量,所以每次调用此方法时都不会在堆栈上创建。在这种情况下,我该如何将m_guard添加到它。例如
boost::recursive_mutex SomeClass::m_guard; //This is static
boost::lock_guard<boost::recursive_mutex> SomeClass::lock //Suppose this is static
void SomeClass::methodA()
{
{
//How do i make lock "lock" mguard
}
}
我从来没有机会使用atomic_flag它是在'atomic.hpp'吗? – Rajeshwar 2013-04-11 05:05:16
yes - http://www.boost.org/doc/libs/1_53_0/boost/atomic/atomic.hpp – 2013-04-11 05:33:14
感谢您的回答 – Rajeshwar 2013-04-11 05:34:25