51 lines
1.1 KiB
C++
51 lines
1.1 KiB
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
|
|
|
|
namespace Dang {
|
|
class Worker {
|
|
public:
|
|
Worker() {
|
|
this->ready = false;
|
|
//init();
|
|
};
|
|
|
|
void run() {
|
|
std::unique_lock<std::mutex> lock(this->mtx);
|
|
this->cv.wait(lock, [this] { return this->ready; }); // Wait until 'ready' is set to true
|
|
std::cout << "Daemon process woke up!" << std::endl;
|
|
};
|
|
|
|
int work() {
|
|
std::thread daemon([this] {this->run();});
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(5));
|
|
{
|
|
std::lock_guard<std::mutex> lock(this->mtx);
|
|
this->ready = true;
|
|
}
|
|
this->cv.notify_one();
|
|
|
|
daemon.join();
|
|
return 0;
|
|
};
|
|
|
|
private:
|
|
std::mutex mtx;
|
|
std::condition_variable cv;
|
|
bool ready;
|
|
};
|
|
}
|
|
|
|
|
|
int main() {
|
|
Dang::Worker worker;
|
|
return worker.work();
|
|
}
|
|
|
|
// vim:set sts=2 sw=2 et:
|