STL condition_variable一个bug

13 篇文章 0 订阅
  1. 测试STL的 condition_variable 发现一个BUG
    VS2019运行如下代码时
// condition_variable::wait_for example
#include <iostream>           // std::cout
#include <thread>             // std::thread
#include <chrono>             // std::chrono::seconds
#include <mutex>              // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variable, std::cv_status

std::condition_variable cv;

int value;

void read_value() {
  std::cin >> value;
  cv.notify_one();
}

int main ()
{
  std::cout << "Please, enter an integer (I'll be printing dots): \n";
  std::thread th (read_value);

  std::mutex mtx;
  std::unique_lock<std::mutex> lck(mtx);
  while (cv.wait_for(lck,std::chrono::seconds(1))==std::cv_status::timeout) {
    std::cout << '.' << std::endl;
  }
  std::cout << "You entered: " << value << '\n';

  th.join();

  return 0;
}

达不到预期效果,不输入字符的时候,wait_for会返回0

  1. 用boost库替代后,发现没有问题,怀疑是MS STL的bug,
    然后下载STL代码,检查代码后,发现问题可能出在事件同步函数中
    stl_condition_variable_concrt
    _CONCRTIMP bool wait_for(::Concurrency::critical_section& _Lck, unsigned int _Timeout = COOPERATIVE_TIMEOUT_INFINITE);

  2. 查看ISSUE后,发现在18年,这个BUG就提交了,只是由于二进制兼容性问题,一直没有修复!

https://developercommunity.visualstudio.com/content/problem/193041/meow.html

ISSUE地址
https://github.com/microsoft/STL/issues/1255

所以,最新的东西可能不是那么稳定,不要随便尝鲜,还是使用稳定版本好。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值