简体   繁体   English

GCC 4.9.2 / GCC 4.8.1-std :: condition_variable :: wait_until(...)错误?

[英]GCC 4.9.2 / GCC 4.8.1 - std::condition_variable::wait_until(…) bug?

I have a problem with the method wait_until on c++11 conditional variables. 我对c ++ 11条件变量的方法wait_until有问题。 It looks like the method return std::cv_status::no_timeout even if there are no notifications. 看起来即使没有通知,方法也返回std :: cv_status :: no_timeout。 The code below shows the problem. 下面的代码显示了该问题。

There are comments in the code below illustrating the problem. 下面的代码中有注释来说明问题。

Compiler used: gcc 4.9.2 (on arch linux) gcc 4.8.1 (on ubuntu 14.04) 使用的编译器:gcc 4.9.2(在arch linux上)gcc 4.8.1(在ubuntu 14.04上)

I am greatful for any help i can get to solve this. 我非常感谢我能解决的任何帮助。

Best regars, Mats 最好的国王,垫子

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


std::mutex m;
std::condition_variable v;

void test_wait_until(int ms)
{
    std::unique_lock<std::mutex> lock(m);

    std::cout << ms << "ms start\n";
    auto expires = std::chrono::system_clock::now() + std::chrono::milliseconds(ms);
    bool run = true;

    do
    {
        // This loop will run at 100% cpu time until
        // the timeout expires.

        auto status = v.wait_until(lock, expires);

        if(status == std::cv_status::timeout){
            std::cout << ms << "ms expired\n";
            run=false;
        }

        if(status == std::cv_status::no_timeout){
            // If the commend below is removed the
            // termial will be filled by the printout.
            // until the timeout expires.

            //std::cout << ms << "ms did not expire\n";
        }
    }while(run);
}


int main()
{
    test_wait_until(20000);
    test_wait_until( 5000);
    test_wait_until(  100);
    test_wait_until(  100);
    test_wait_until(   10);
    test_wait_until(    0);
    test_wait_until(   -10);
    test_wait_until(  -100);
    test_wait_until(  -100);
    test_wait_until( -5000);
    test_wait_until(-20000);
}

You probably need to build your executable with threading support. 您可能需要使用线程支持来构建可执行文件。 On Linux with gcc 4.9.2 this would go something like this: 在具有gcc 4.9.2的Linux上,这将是这样的:

g++ -std=c++11 test.cpp -o test -pthread

Ok the problem was: I / we were using the -lpthread option instead of the -pthread option for gcc. 好的,问题是:我/我们在gcc中使用-lpthread选项而不是-pthread选项。

The strange this is other things works rather well, but this seams to the solution. 奇怪的是,其他的事情效果很好,但这正解决了问题。

Thank you all! 谢谢你们!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 std :: condition_variable :: wait_until的实现 - implementation of std::condition_variable::wait_until std :: condition_variable :: wait_until函数 - std::condition_variable::wait_until function 如何工作 std::condition_variable::wait_until - How works std::condition_variable::wait_until std :: condition_variable :: wait_for和std :: condition_variable :: wait_until有什么区别? - What is the difference between std::condition_variable::wait_for and std::condition_variable::wait_until? 系统时间更改的 wait_until 的 condition_variable 解决方法 - condition_variable workaround for wait_until with system time change std :: condition_variable :: wait_until对std :: this_thread :: sleep_for有什么好处吗? - Does std::condition_variable::wait_until have any advantage against std::this_thread::sleep_for? condition_variable::wait_until 意外通过 g++ (9.4.0) - condition_variable::wait_until passes through unexpectedly g++ (9.4.0) C++ 11 - condition_variable - wait_until 没有按预期工作 - C++ 11 - condition_variable - wait_until not working as expected 使用chrono :: steady_clock使用condition_variable :: wait_for和wait_until,但是在PC睡眠时跳过持续时间? - condition_variable::wait_for and wait_until using chrono::steady_clock but skipping duration while PC is asleep? 条件变量:: wait_for中的gcc错误 - gcc bug in condition variable::wait_for
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM