[英]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.