Skip to content

Update condition_variable.cpp #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/condition_variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,22 @@
// Includes the thread library header.
#include <thread>

using namespace std;

// Defining a global count variable, a mutex, and a condition variable to
// be used by both threads.
int count = 0;
std::mutex m;
mutex m;

// This is the syntax for declaring and default initializing a condition
// variable.
std::condition_variable cv;
condition_variable cv;

// In this function, a thread increments the count variable by
// 1. It also will notify one waiting thread if the count value is 2.
// It is ran by two of the threads in the main function.
void add_count_and_notify() {
std::scoped_lock slk(m);
scoped_lock slk(m);
count += 1;
if (count == 2) {
cv.notify_one();
Expand All @@ -58,10 +60,10 @@ void add_count_and_notify() {
// condition variables. Particularly, it is moveable but not copy-constructible
// or copy-assignable.
void waiter_thread() {
std::unique_lock lk(m);
unique_lock lk(m);
cv.wait(lk, []{return count == 2;});

std::cout << "Printing count: " << count << std::endl;
cout << "Printing count: " << count << endl;
}

// The main method constructs three thread objects and has two of them run the
Expand All @@ -70,11 +72,11 @@ void waiter_thread() {
// both increments, along with the conditional acquisition in the waiter
// thread, worked successfully.
int main() {
std::thread t1(add_count_and_notify);
std::thread t2(add_count_and_notify);
std::thread t3(waiter_thread);
thread t1(add_count_and_notify);
thread t2(add_count_and_notify);
thread t3(waiter_thread);
t1.join();
t2.join();
t3.join();
return 0;
}
}