Last active
March 17, 2023 11:07
-
-
Save joshmh/bec4a312e5d29151f329e23daa879804 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <mutex> | |
#include <condition_variable> | |
#include <thread> | |
#include <chrono> | |
#include <string> | |
#include <iostream> | |
std::atomic<bool> sigg; | |
std::string s = "na"; | |
std::mutex m; | |
std::condition_variable v; | |
bool pred() {return sigg;} | |
void a() { | |
std::unique_lock<std::mutex> lock(m); | |
printf("waiting for cv...\n"); | |
v.wait(lock, pred); | |
std::cout << "Finished waiting: " << s << "\n"; | |
} | |
void b() { | |
std::unique_lock<std::mutex> lock(m); | |
sigg = true; | |
printf("doing something...\n"); | |
std::this_thread::sleep_for(std::chrono::seconds(3)); | |
s = "hello world"; | |
printf("notifying\n"); | |
v.notify_one(); | |
} | |
int main() { | |
std::thread t1(a), t2(b); | |
t1.join(); | |
t2.join(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment