我第一次使用std::timed_mutex
并且它的行为不符合我的预期。它似乎立即失败而不是等待互斥锁。我以毫秒为单位提供锁定超时(如http://www.cplusplus.com/reference/mutex/timed_mutex/try_lock_for/所示)。但是对try_lock_for()
的调用立即失败。
这是处理锁定和解锁互斥锁的类:
const unsigned int DEFAULT_MUTEX_WAIT_TIME_MS = 5 * 60 * 1000;
class ScopedTimedMutexLock
{
public:
ScopedTimedMutexLock(std::timed_mutex* sourceMutex, unsigned int numWaitMilliseconds=DEFAULT_MUTEX_WAIT_TIME_MS)
m_mutex(sourceMutex)
{
if( !m_mutex->try_lock_for( std::chrono::milliseconds(numWaitMilliseconds) ) )
{
std::string message = "Timeout attempting to acquire mutex lock for ";
message += Conversion::toString(numWaitMilliseconds);
message += "ms";
throw MutexException(message);
}
}
~ScopedTimedMutexLock()
{
m_mutex->unlock();
}
private:
std::timed_mutex* m_mutex;
};
这就是它被使用的地方:
void CommandService::Process( RequestType& request )
{
unsigned long callTime =
std::chrono::duration_cast< std::chrono::milliseconds >(
std::chrono::system_clock::now().time_since_epoch()
).count();
try
{
ScopedTimedMutexLock lock( m_classMutex, request.getLockWaitTimeMs(DEFAULT_MUTEX_WAIT_TIME_MS) );
// ... command processing code goes here
}
catch( MutexException& mutexException )
{
unsigned long catchTime =
std::chrono::duration_cast< std::chrono::milliseconds >(
std::chrono::system_clock::now().time_since_epoch()
).count();
cout << "The following error occured while attempting to process command"
<< "\n call time: " << callTime
<< "\n catch time: " << catchTime;
cout << mutexException.description();
}
}
这是控制台输出:
The following error occured while attempting to process command
call time: 1131268914
catch time: 1131268914
Timeout attempting to acquire mutex lock for 300000ms
知道这出错了吗?转换为std::chrono::milliseconds
是否正确?如何让try_lock_for()
等待锁定?
其他信息:对try_lock_for()
的调用并非始终立即失败。很多时候,电话获得了锁定,一切都按预期工作。我看到的失败是间歇性的。请参阅下面的答案,了解有关失败原因的详细信息。
答案 0 :(得分:0)
http://en.cppreference.com/w/cpp/thread/timed_mutex/try_lock_for的try_lock_for()
说明中提到了问题的根本原因。在描述的最后,它说:
与try_lock()一样,允许此函数虚假地失败 即使互斥锁未被任何其他线程锁定,也返回false 在timeout_duration期间的某个点。
我天真地假设只有两种可能的结果:(1)函数在时间段内获得锁定,或(2)在等待时间过去后函数失败。但还有另一种可能性,(3)在没有特定原因的情况下,功能在相对较短的时间后失效。 TL; DR,我的坏。
我通过重写ScopedTimedMutexLock
构造函数来循环try_lock()
来解决问题,直到获得锁定或超过等待时间限制。
ScopedTimedMutexLock(std::timed_mutex* sourceMutex, unsigned int numWaitMilliseconds=DEFAULT_MUTEX_WAIT_TIME_MS)
m_mutex(sourceMutex)
{
const unsigned SLEEP_TIME_MS = 5;
bool isLocked = false;
unsigned long startMS = now();
while( now() - startMS < numWaitMilliseconds && !isLocked )
{
isLocked = m_sourceMutex->try_lock();
if( !isLocked )
{
std::this_thread::sleep_for(
std::chrono::milliseconds(SLEEP_TIME_MS));
}
}
if( !isLocked )
{
std::string message = "Timeout attempting to acquire mutex lock for ";
message += Conversion::toString(numWaitMilliseconds);
message += "ms";
throw MutexException(message);
}
}
now()
的定义如下:
private:
unsigned long now() {
return std::chrono::duration_cast< std::chrono::milliseconds >(
std::chrono::system_clock::now().time_since_epoch() ).count();
}