簡體   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