#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
int count;
std::mutex count_mutex;
std::condition_variable_any count_cv;
void thread_start()
{
count_mutex.lock();
count_cv.wait(count_mutex, []{return count >= 10000;});
int val = count;
count_mutex.unlock();
std::cout << "val = " << val << std::endl;
}
int main()
{
std::thread t1(thread_start);
for (int i = 0; i < 100000; ++i)
{
// These three lines:
count_mutex.lock();
count = i;
count_mutex.unlock();
// count was changed, so wake up t1 and have it check the condition again.
count_cv.notify_one();
}
t1.join();
}
I want to compile and run this
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.