c++ - boost - timed_wait 不等待

标签 c++ boost linux-kernel futex

我在 Linux 操作系统 + ARM 处理器 + boost 1.51 上运行以下代码。但是,代码没有按预期工作,timed_wait() 调用立即返回。

#include <boost/thread/condition.hpp>
#include <boost/thread/xtime.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream>

using namespace std;

int main()
{
     boost::mutex mutex_;
     boost::mutex::scoped_lock lock( mutex_ );

     boost::xtime xt;
     boost::condition condition;

     // wait for one second or wait on lock
     boost::xtime_get(&xt, boost::TIME_UTC_);

     xt.sec += 1;

     cout << "Before 1 second wait" << endl;
     condition.timed_wait(lock, xt);
     cout << "After 1 second wait" << endl;

     return 0;
}

在具有相同 ARM 处理器但不同版本的 Linux + glibc + 相同的 boost 1.51 库的其他系统上,代码运行正常并等待 1 秒。

我尝试使用 strace 调试问题。我看到一个不同之处,即在它不工作的系统中没有调用 futex()。

来自代码运行的系统的 strace:

write(1, "Before 1 second wait\n", 21Before 1 second wait)  = 21
futex(0xb6fbf0dc, FUTEX_WAKE_PRIVATE, 2147483647) = 0
clock_gettime(CLOCK_REALTIME, {1438150496, 732211544}) = 0
futex(0xbef07a44, FUTEX_WAIT_PRIVATE, 1, {0, 998193456}) = -1 ETIMEDOUT (Connection timed out)
futex(0xbef07a28, FUTEX_WAKE_PRIVATE, 1) = 0
write(1, "After 1 second wait\n", 20After 1 second wait)   = 20

来自代码不工作的系统的 strace:

    write(1, "Before 1 second wait\n", 21Before 1 second wait)  = 21
    futex(0xb6fc90dc, FUTEX_WAKE_PRIVATE, 2147483647) = 0
    clock_gettime(CLOCK_REALTIME, {1438150407, 134963583}) = 0
    futex(0xbe9be988, FUTEX_WAKE_PRIVATE, 1) = 0
    write(1, "After 1 second wait\n", 20After 1 second wait)   = 20

是否需要更改内核/glibc 才能使此代码正常工作?

最佳答案

与其在使用超时时弄乱时钟,不如使用实际的超时?

condition.timed_wait(lock,boost::posix_time::milliseconds(1000))

这可以防止各种奇怪的问题。

关于c++ - boost - timed_wait 不等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31695487/

相关文章:

c++ - 类型转换、显式构造函数等

c++ - 退出码3(不是我的返回值,找源码)

linux-kernel - 如何从 Linux 内核中的任何地址获取结构页面

c - qemu 中的启动参数

linux - HR 计时器精度研究案例

c++ - 将 boost::optional 与常量类型一起使用 - C++

c++ - 如何在运行时使用c++在工厂类中动态注册类

c++ vector 列表初始值设定项不适用于我的类的类型转换构造函数

c++ - 替代宏以帮助类型安全和减少重复

c++ - 'reinterpret_cast' : cannot convert from 'overloaded-function' to 'intptr_t' with boost. dll