56e97255ed4dee2f31b7100264a4113ea3218463
David Blume Experiment with a manager/w...

David Blume authored 8 years ago

1) #include <iostream>
2) #include <semaphore.h>
3) #include "thread_manager.hpp"
4) 
5) // using namespace std;
6) using std::condition_variable;
7) using std::cout;
8) using std::endl;
dblume Add .clang-format

dblume authored 1 year ago

9) using std::lock_guard;
10) using std::mutex;
11) using std::thread;
12) using std::unique_lock;
13) using std::chrono::seconds;
David Blume Experiment with a manager/w...

David Blume authored 8 years ago

14) 
15) /*
16) class Reporter {
dblume Remove declaration tags. Be...

dblume authored 2 years ago

17)     // TODO: Move all the reporting stuff over to this class.
David Blume Experiment with a manager/w...

David Blume authored 8 years ago

18) };
19) 
20) class Worker {
dblume Remove declaration tags. Be...

dblume authored 2 years ago

21)     Worker(): count_(0) {}
22)     void work();
David Blume Experiment with a manager/w...

David Blume authored 8 years ago

23) 
dblume Remove declaration tags. Be...

dblume authored 2 years ago

24)     int count_;
David Blume Experiment with a manager/w...

David Blume authored 8 years ago

25) };
26) */
27) 
dblume Add .clang-format

dblume authored 1 year ago

28) Manager::Manager() : abort_reporting_(false), data_to_protect_(0)
David Blume Experiment with a manager/w...

David Blume authored 8 years ago

29) {
dblume Add .clang-format

dblume authored 1 year ago

30)     report_thread_ = thread([this] { WriteReport(); });
David Blume Experiment with a manager/w...

David Blume authored 8 years ago

31) }
32) 
33) Manager::~Manager()
34) {
dblume Remove declaration tags. Be...

dblume authored 2 years ago

35)     abort_reporting_ = true;
36)     report_condition_.notify_all();
37)     report_thread_.join();
David Blume Experiment with a manager/w...

David Blume authored 8 years ago

38) }
39) 
40) void Manager::DeployWorker()
41) {
dblume Remove declaration tags. Be...

dblume authored 2 years ago

42)     // See http://preshing.com/20150316/semaphores-are-surprisingly-versatile/
43)     // And https://github.com/preshing/cpp11-on-multicore
dblume Add .clang-format

dblume authored 1 year ago

44)     //  unique_lock<Semaphore> sem(thread_counter_sem);
45)     //  thread(&Manager::work, this, std::move(sem)).detach();
David Blume Experiment with a manager/w...

David Blume authored 8 years ago

46) }
47) 
48) /*
49) void Manager::work(unique_lock<Semaphore> //sem
50)                                                 )
51) {
52)     cout << "Work here." << endl;
53) }
54) */
55) 
56) void Manager::WriteReport()
57) {
dblume Remove declaration tags. Be...

dblume authored 2 years ago

58)     const seconds duration(5);
59)     mutex timer_mutex;
60)     while (!abort_reporting_) {
61)         unique_lock<mutex> timeout(timer_mutex);
62)         if (report_condition_.wait_for(timeout, duration,
dblume Add .clang-format

dblume authored 1 year ago

63)                                        [this]() { return abort_reporting_; })) {
dblume Remove declaration tags. Be...

dblume authored 2 years ago

64)             return;
65)         }
66)         lock_guard<mutex> crit_sec(report_lock_);
67)         cout << "This is the report: " << data_to_protect_ << endl;
68)     }