gpt4 book ai didi

c++ - std::timed_mutex::try_lock_for 立即失败

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:43:31 27 4
gpt4 key购买 nike

我第一次使用 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() 的调用并不总是立即失败。多次调用获得了锁,一切都按预期进行。我看到的失败是断断续续的。有关失败原因的详细信息,请参阅下面的回答。

最佳答案

问题的根本原因在 http://en.cppreference.com/w/cpp/thread/timed_mutex/try_lock_fortry_lock_for() 描述中提到.在描述的末尾,它说:

As with try_lock(), this function is allowed to fail spuriously and return false even if the mutex was not locked by any other thread at some point during 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();
}

关于c++ - std::timed_mutex::try_lock_for 立即失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44190865/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com