tmp.c/worker.cpp
Vincent Batts d3a10d7190
*: adding a threaded worker example
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2025-02-08 19:56:52 -05:00

32 lines
756 B
C++

#include <iostream>
#include <thread>
#include <condition_variable>
#include <mutex>
// https://en.cppreference.com/w/cpp/thread/thread
// https://en.cppreference.com/w/cpp/thread/mutex
// https://en.cppreference.com/w/cpp/thread/condition_variable
std::mutex mtx;
std::condition_variable cv;
bool ready = false;
void worker() {
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [] { return ready; }); // Wait until 'ready' is set to true
std::cout << "Daemon process woke up!" << std::endl;
}
int main() {
std::thread daemon(worker);
std::this_thread::sleep_for(std::chrono::seconds(5));
{
std::lock_guard<std::mutex> lock(mtx);
ready = true;
}
cv.notify_one();
daemon.join();
return 0;
}