C++ condition variable doesn’t block the thread on wait_until() using the time_poin::max()?

The below code doesn’t block the thread and it gets into an infinite loop and keep printing

Waiting…

Waiting…

My idea is, the thread should block on the max() time and when it’s set to now(), it should unblock the thread and do the business. Any ideas? I am using VS2013

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include "stdafx.h"
#include <iostream>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <chrono>
std::condition_variable condVar;
std::mutex mut;
bool pred = false;
auto dueAt = std::chrono::steady_clock::time_point::max();
void threadFunc()
{
for (;;)
{
std::unique_lock<std::mutex> lock(mut);
dueAt = std::chrono::steady_clock::time_point::max();
if (condVar.wait_until(lock, dueAt, [](){ return pred; }))
{
std::cout << "Ready..." << std::endl;
pred = false;
dueAt = std::chrono::steady_clock::time_point::max();
}
else
{
std::cout << "Waiting..." << std::endl;
}
}
}
void notifierThread()
{
for (int i = 0; i < 5; i++)
{
std::this_thread::sleep_for(std::chrono::seconds(5));
{
std::lock_guard<std::mutex> lock(mut);
dueAt = std::chrono::steady_clock::now();
pred = true;
std::cout << " Signalling..." << std::endl;
}
condVar.notify_one();
}
}
int _tmain(int argc, _TCHAR* argv[])
{
std::thread t1(threadFunc);
std::thread t2(notifierThread);
t1.join();
t2.join();
return 0;
}
</code>
<code>#include "stdafx.h" #include <iostream> #include <condition_variable> #include <mutex> #include <thread> #include <chrono> std::condition_variable condVar; std::mutex mut; bool pred = false; auto dueAt = std::chrono::steady_clock::time_point::max(); void threadFunc() { for (;;) { std::unique_lock<std::mutex> lock(mut); dueAt = std::chrono::steady_clock::time_point::max(); if (condVar.wait_until(lock, dueAt, [](){ return pred; })) { std::cout << "Ready..." << std::endl; pred = false; dueAt = std::chrono::steady_clock::time_point::max(); } else { std::cout << "Waiting..." << std::endl; } } } void notifierThread() { for (int i = 0; i < 5; i++) { std::this_thread::sleep_for(std::chrono::seconds(5)); { std::lock_guard<std::mutex> lock(mut); dueAt = std::chrono::steady_clock::now(); pred = true; std::cout << " Signalling..." << std::endl; } condVar.notify_one(); } } int _tmain(int argc, _TCHAR* argv[]) { std::thread t1(threadFunc); std::thread t2(notifierThread); t1.join(); t2.join(); return 0; } </code>
#include "stdafx.h"
#include <iostream>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <chrono>

std::condition_variable condVar;
std::mutex mut;
bool pred = false;
auto dueAt = std::chrono::steady_clock::time_point::max();

void threadFunc()
{
    for (;;)
    {
        std::unique_lock<std::mutex> lock(mut);
        dueAt = std::chrono::steady_clock::time_point::max();

        if (condVar.wait_until(lock, dueAt, [](){ return pred; }))
        {
            std::cout << "Ready..." << std::endl;
            pred = false;
            dueAt = std::chrono::steady_clock::time_point::max();
        }
        else
        {
            std::cout << "Waiting..." << std::endl;
        }
    }
}

void notifierThread()
{
    for (int i = 0; i < 5; i++)
    {
        std::this_thread::sleep_for(std::chrono::seconds(5));
        {
            std::lock_guard<std::mutex> lock(mut);
            dueAt = std::chrono::steady_clock::now();
            pred = true;
            std::cout << " Signalling..." << std::endl;
        }
        condVar.notify_one();
    }
}


int _tmain(int argc, _TCHAR* argv[])
{
    std::thread t1(threadFunc);
    std::thread t2(notifierThread);
    t1.join();
    t2.join();
    return 0;
}

11

Your compiler is old and has bugs?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <iostream>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <chrono>
std::condition_variable condVar;
std::mutex mut;
bool pred = false;
bool shutdown = false;
int result = 0;
void threadFunc()
{
for (;;)
{
std::unique_lock<std::mutex> lock(mut);
condVar.wait(lock, [](){ return pred || shutdown; });
if (shutdown) {
std::cout << "Shutdown!" << std::endl;
break;
}
std::cout << "Ready... #" << result << std::endl;
pred = false;
++result;
}
}
void notifierThread()
{
for (int i = 0; i < 5; i++)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
{
std::lock_guard<std::mutex> lock(mut);
pred = true;
std::cout << " Signalling... #" << i << std::endl;
}
condVar.notify_one();
}
}
int main()
{
std::thread t1(threadFunc);
std::thread t2(notifierThread);
t2.join();
while (true) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::lock_guard<std::mutex> l(mut);
if (result == 5)
{
std::cout << " Signalling Shutdown!" << std::endl;
shutdown = true;
break;
}
}
condVar.notify_one();
t1.join();
return 0;
}
</code>
<code>#include <iostream> #include <condition_variable> #include <mutex> #include <thread> #include <chrono> std::condition_variable condVar; std::mutex mut; bool pred = false; bool shutdown = false; int result = 0; void threadFunc() { for (;;) { std::unique_lock<std::mutex> lock(mut); condVar.wait(lock, [](){ return pred || shutdown; }); if (shutdown) { std::cout << "Shutdown!" << std::endl; break; } std::cout << "Ready... #" << result << std::endl; pred = false; ++result; } } void notifierThread() { for (int i = 0; i < 5; i++) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); { std::lock_guard<std::mutex> lock(mut); pred = true; std::cout << " Signalling... #" << i << std::endl; } condVar.notify_one(); } } int main() { std::thread t1(threadFunc); std::thread t2(notifierThread); t2.join(); while (true) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::lock_guard<std::mutex> l(mut); if (result == 5) { std::cout << " Signalling Shutdown!" << std::endl; shutdown = true; break; } } condVar.notify_one(); t1.join(); return 0; } </code>
#include <iostream>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <chrono>

std::condition_variable condVar;
std::mutex mut;
bool pred = false;
bool shutdown = false;
int result = 0;

void threadFunc()
{
  for (;;)
  {
    std::unique_lock<std::mutex> lock(mut);

    condVar.wait(lock, [](){ return pred || shutdown; });

    if (shutdown) {
      std::cout << "Shutdown!" << std::endl;
      break;
    }
    std::cout << "Ready... #" << result << std::endl;
    pred = false;
    ++result;
  }
}

void notifierThread()
{
  for (int i = 0; i < 5; i++)
  {
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    {
      std::lock_guard<std::mutex> lock(mut);
      pred = true;
      std::cout << " Signalling... #" << i << std::endl;
    }
    condVar.notify_one();
  }
}


int main()
{
    std::thread t1(threadFunc);
    std::thread t2(notifierThread);
    t2.join();
    while (true) {
      std::this_thread::sleep_for(std::chrono::milliseconds(100));
      std::lock_guard<std::mutex> l(mut);
      if (result == 5)
      {
        std::cout << " Signalling Shutdown!" << std::endl;
        shutdown = true;
        break;
      }
    }
    condVar.notify_one();
    t1.join();
    return 0;
}

here is a complete terminating version of your program that does not use dueAt.

Manipulating dueAt while you are .wait_untiling should do nothing (barring UB due to a race condition, which you avoided with a lock). It appears your C++ std implementation has a bug.

What, exactly, you where trying to do isn’t clear, so I converted your code into something that (probably) halts.

In practice, you can’t assume that sending 5 signals this way results in 5 receptions, as the sleep(100 ms) isn’t guaranteed to give the reader thread to read. A more proper program would send a counter:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <iostream>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <chrono>
std::condition_variable condVar;
std::mutex mut;
int counter = 0;
bool shutdown = false;
int consumed = 0;
void threadFunc()
{
for (;;)
{
std::unique_lock<std::mutex> lock(mut);
auto old_count = counter;
condVar.wait(lock, [old_count](){ return (counter != old_count) || shutdown; });
if (shutdown) {
std::cout << "Shutdown!" << std::endl;
break;
}
std::cout << "Ready... #" << old_count << ".." << counter << std::endl;
consumed = counter;
}
}
void notifierThread()
{
std::this_thread::sleep_for(std::chrono::milliseconds(250));
for (int i = 0; i < 5; i++)
{
if (i%2)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
{
std::lock_guard<std::mutex> lock(mut);
++counter;
std::cout << " Signalling... #" << i << " with count " << counter << std::endl;
}
condVar.notify_one();
}
}
int main()
{
std::thread t1(threadFunc);
std::thread t2(notifierThread);
t2.join();
while (true) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::lock_guard<std::mutex> l(mut);
if (consumed == 5)
{
std::cout << " Signalling Shutdown!" << std::endl;
shutdown = true;
break;
}
}
condVar.notify_one();
t1.join();
return 0;
}
</code>
<code>#include <iostream> #include <condition_variable> #include <mutex> #include <thread> #include <chrono> std::condition_variable condVar; std::mutex mut; int counter = 0; bool shutdown = false; int consumed = 0; void threadFunc() { for (;;) { std::unique_lock<std::mutex> lock(mut); auto old_count = counter; condVar.wait(lock, [old_count](){ return (counter != old_count) || shutdown; }); if (shutdown) { std::cout << "Shutdown!" << std::endl; break; } std::cout << "Ready... #" << old_count << ".." << counter << std::endl; consumed = counter; } } void notifierThread() { std::this_thread::sleep_for(std::chrono::milliseconds(250)); for (int i = 0; i < 5; i++) { if (i%2) std::this_thread::sleep_for(std::chrono::milliseconds(100)); { std::lock_guard<std::mutex> lock(mut); ++counter; std::cout << " Signalling... #" << i << " with count " << counter << std::endl; } condVar.notify_one(); } } int main() { std::thread t1(threadFunc); std::thread t2(notifierThread); t2.join(); while (true) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::lock_guard<std::mutex> l(mut); if (consumed == 5) { std::cout << " Signalling Shutdown!" << std::endl; shutdown = true; break; } } condVar.notify_one(); t1.join(); return 0; } </code>
#include <iostream>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <chrono>

std::condition_variable condVar;
std::mutex mut;
int counter = 0;
bool shutdown = false;
int consumed = 0;

void threadFunc()
{
  for (;;)
  {
    std::unique_lock<std::mutex> lock(mut);

    auto old_count = counter;
    condVar.wait(lock, [old_count](){ return (counter != old_count) || shutdown; });

    if (shutdown) {
      std::cout << "Shutdown!" << std::endl;
      break;
    }
    std::cout << "Ready... #" << old_count << ".." << counter << std::endl;
    consumed = counter;
  }
}

void notifierThread()
{
  std::this_thread::sleep_for(std::chrono::milliseconds(250));
  for (int i = 0; i < 5; i++)
  {
    if (i%2)
      std::this_thread::sleep_for(std::chrono::milliseconds(100));
    {
      std::lock_guard<std::mutex> lock(mut);
      ++counter;
      std::cout << " Signalling... #" << i << " with count " << counter << std::endl;
    }
    condVar.notify_one();
  }
}


int main()
{
    std::thread t1(threadFunc);
    std::thread t2(notifierThread);
    t2.join();
    while (true) {
      std::this_thread::sleep_for(std::chrono::milliseconds(100));
      std::lock_guard<std::mutex> l(mut);
      if (consumed == 5)
      {
        std::cout << " Signalling Shutdown!" << std::endl;
        shutdown = true;
        break;
      }
    }
    condVar.notify_one();
    t1.join();
    return 0;
}

here I make the producer be erratic, sometimes fast and sometimes slow. The consumer can handle more than 1 signal at a time however.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật