David Blume's GitList
Repositories
testcode.git
Code
Commits
Branches
Tags
Search
Tree:
b2afb67
Branches
Tags
c++11
main
start
testcode.git
product
thread_manager.cpp
Experiment with a manager/worker scenario.
David Blume
commited
b2afb67
at 2016-02-07 22:00:00
thread_manager.cpp
Blame
History
Raw
#include <iostream> #include <semaphore.h> #include "thread_manager.hpp" // using namespace std; using std::thread; using std::mutex; using std::condition_variable; using std::chrono::seconds; using std::unique_lock; using std::lock_guard; using std::cout; using std::endl; /* 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; } }