*: adding a threaded worker example
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
parent
c89b8f22da
commit
d3a10d7190
4 changed files with 44 additions and 1 deletions
32
worker.cpp
Normal file
32
worker.cpp
Normal file
|
@ -0,0 +1,32 @@
|
|||
#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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue