为什么std :: timed_mutex :: try_lock_for不起作用?

mis*_*coo 6 c++ c++11

我使用gcc-4.8.1(configure:./ configure --prefix =/usr/local)在Ubuntu 12.04中编译以下代码,但是当我运行它时,它不起作用.它没有停下来等待互斥锁.它返回了假,并且输出了"Hello world!"

命令:g ++ -std = c ++ 11 main.cpp -omain -pthread

当我使用gcc-4.6(apt-get install g ++)编译它时,它运行良好.该节目等了大约十秒钟,并且超过了"Hello world!"

#include <thread>
#include <iostream>
#include <chrono>
#include <mutex>

std::timed_mutex test_mutex;

void f()
{
    test_mutex.try_lock_for(std::chrono::seconds(10));
    std::cout << "hello world\n";
}

int main()
{
    std::lock_guard<std::timed_mutex> l(test_mutex);
    std::thread t(f);
    t.join();
      return 0;
}
Run Code Online (Sandbox Code Playgroud)

Jes*_*ood 6

如果我没有弄错的话,那就是Bug 54562 -mutex和条件变量计时器.

还提到了bug的原因:

这是因为它使用CLOCK_MONOTONIC时钟(如果在平台上可用)来计算需要返回的绝对时间,这是不正确的,因为POSIX pthread_mutex_timedlock()调用使用CLOCK_REALTIME时钟,并且在我的平台上单调时钟是方式在实时时钟后面.

但是,这并不能解释为什么你会看到正确的行为gcc-4.6.也许_GLIBCXX_USE_CLOCK_MONOTONIC没有启用?