tmp.c/worker.h
Vincent Batts 7628ecc915
main: :ohno: linked to libcurl and did a webconnect
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2025-02-10 17:58:07 -05:00

64 lines
1.3 KiB
C++

// Copyright 2025 Vincent Batts <vbatts@hashbangbash.com>
// 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
#ifndef WORKER_H_
#define WORKER_H_
#include <iostream>
#include <thread>
#include <condition_variable>
#include <mutex>
namespace Dang {
class Worker {
public:
Worker() {
this->ready = false;
this->timeout = 5;
}
void run() {
std::unique_lock<std::mutex> lock(this->mtx);
// Wait until 'ready' is set to true
this->cv.wait(lock, [this] { return this->ready; });
std::cout << "Daemon process woke up!" << std::endl;
}
int getTimeout() {
return this->timeout;
}
void setTimeout(int t) {
this->timeout = t;
}
int work() {
std::thread daemon([this] {this->run();});
std::this_thread::sleep_for(std::chrono::seconds(this->timeout));
{
std::lock_guard<std::mutex> lock(this->mtx);
this->ready = true;
}
this->cv.notify_one();
daemon.join();
return 0;
}
private:
int timeout;
std::mutex mtx;
std::condition_variable cv;
bool ready;
};
} // namespace Dang
#endif // WORKER_H_
// vim:set sts=2 sw=2 et: