Skip to content

Instantly share code, notes, and snippets.

@joshmh
Last active March 17, 2023 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshmh/bec4a312e5d29151f329e23daa879804 to your computer and use it in GitHub Desktop.
Save joshmh/bec4a312e5d29151f329e23daa879804 to your computer and use it in GitHub Desktop.
#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