繁体   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?

我对c ++ 11条件变量的方法wait_until有问题。 看起来即使没有通知,方法也返回std :: cv_status :: no_timeout。 下面的代码显示了该问题。

下面的代码中有注释来说明问题。

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

我非常感谢我能解决的任何帮助。

最好的国王,垫子

#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);
}

您可能需要使用线程支持来构建可执行文件。 在具有gcc 4.9.2的Linux上,这将是这样的:

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

好的,问题是:我/我们在gcc中使用-lpthread选项而不是-pthread选项。

奇怪的是,其他的事情效果很好,但这正解决了问题。

谢谢你们!

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM