David Blume's GitList
Repositories
testcode.git
Code
Commits
Branches
Tags
Search
Tree:
56e9725
Branches
Tags
c++11
main
start
testcode.git
product
thread_manager.cpp
Add .clang-format
dblume
commited
56e9725
at 2023-08-12 23:24:26
thread_manager.cpp
Blame
History
Raw
#include <iostream> #include <semaphore.h> #include "thread_manager.hpp" // using namespace std; using std::condition_variable; using std::cout; using std::endl; using std::lock_guard; using std::mutex; using std::thread; using std::unique_lock; using std::chrono::seconds; /* class Reporter { // TODO: Move all the reporting stuff over to this class. }; class Worker { Worker(): count_(0) {} void work(); int count_; }; */ Manager::Manager() : abort_reporting_(false), data_to_protect_(0) { report_thread_ = thread([this] { WriteReport(); }); } Manager::~Manager() { abort_reporting_ = true; report_condition_.notify_all(); report_thread_.join(); } void Manager::DeployWorker() { // See http://preshing.com/20150316/semaphores-are-surprisingly-versatile/ // And https://github.com/preshing/cpp11-on-multicore // unique_lock<Semaphore> sem(thread_counter_sem); // thread(&Manager::work, this, std::move(sem)).detach(); } /* void Manager::work(unique_lock<Semaphore> //sem ) { cout << "Work here." << endl; } */ void Manager::WriteReport() { const seconds duration(5); mutex timer_mutex; while (!abort_reporting_) { unique_lock<mutex> timeout(timer_mutex); if (report_condition_.wait_for(timeout, duration, [this]() { return abort_reporting_; })) { return; } lock_guard<mutex> crit_sec(report_lock_); cout << "This is the report: " << data_to_protect_ << endl; } }