Experiment with a manager/worker scenario.
David Blume

David Blume commited on 2016-02-07 22:00:00
Showing 5 changed files, with 105 additions and 2 deletions.

... ...
@@ -0,0 +1 @@
1
+*.exe
... ...
@@ -4,8 +4,8 @@ LIBDIR=../lib
4 4
 LDFLAGS=-pthread -L $(LIBDIR) -lmy_lib
5 5
 OBJDIR=obj
6 6
 
7
-SOURCES=main.cpp main_helper.cpp ui/widget.cpp \
8
-	scoped_set_adder.cpp thread_with_lambda.cpp
7
+SOURCES=main.cpp main_helper.cpp ui/widget.cpp scoped_set_adder.cpp \
8
+	thread_with_lambda.cpp thread_manager.cpp
9 9
 # //OBJECTS=$(OBJDIR)$(SOURCES:.cpp=.o)
10 10
 OBJECTS=$(foreach bname, $(basename $(SOURCES)), $(OBJDIR)/$(bname).o)
11 11
 EXECUTABLE=main
... ...
@@ -10,6 +10,7 @@
10 10
 #include "ui/widget.hpp"
11 11
 #include "../include/my_lib.hpp"
12 12
 #include "thread_with_lambda.hpp"
13
+#include "thread_manager.hpp"
13 14
  
14 15
 using namespace std;
15 16
  
... ...
@@ -0,0 +1,76 @@
1
+#include <iostream>
2
+#include <semaphore.h>
3
+#include "thread_manager.hpp"
4
+
5
+// using namespace std;
6
+using std::thread;
7
+using std::mutex;
8
+using std::condition_variable;
9
+using std::chrono::seconds;
10
+using std::unique_lock;
11
+using std::lock_guard;
12
+using std::cout;
13
+using std::endl;
14
+
15
+
16
+/*
17
+class Reporter {
18
+	// TODO: Move all the reporting stuff over to this class.
19
+};
20
+
21
+class Worker {
22
+	Worker(): count_(0) {}
23
+	void work();
24
+
25
+	int count_;
26
+};
27
+*/
28
+
29
+
30
+Manager::Manager():
31
+	abort_reporting_(false),
32
+	data_to_protect_(0)
33
+{
34
+	report_thread_ = thread([this]{ WriteReport(); });
35
+}
36
+
37
+
38
+Manager::~Manager()
39
+{
40
+	abort_reporting_ = true;
41
+	report_condition_.notify_all();
42
+	report_thread_.join();
43
+}
44
+
45
+
46
+void Manager::DeployWorker()
47
+{
48
+	// See http://preshing.com/20150316/semaphores-are-surprisingly-versatile/
49
+	// And https://github.com/preshing/cpp11-on-multicore
50
+//	unique_lock<Semaphore> sem(thread_counter_sem);
51
+//	thread(&Manager::work, this, std::move(sem)).detach();
52
+}
53
+
54
+/*
55
+void Manager::work(unique_lock<Semaphore> //sem
56
+                                                )
57
+{
58
+    cout << "Work here." << endl;
59
+}
60
+*/
61
+
62
+
63
+void Manager::WriteReport()
64
+{
65
+	const seconds duration(5);
66
+	mutex timer_mutex;
67
+	while (!abort_reporting_) {
68
+		unique_lock<mutex> timeout(timer_mutex);
69
+		if (report_condition_.wait_for(timeout, duration,
70
+			[this]() { return abort_reporting_; })) {
71
+			return;
72
+		}
73
+		lock_guard<mutex> crit_sec(report_lock_);
74
+		cout << "This is the report: " << data_to_protect_ << endl;
75
+	}
76
+}
... ...
@@ -0,0 +1,25 @@
1
+#ifndef thread_manager_hpp_
2
+#define thread_manager_hpp_
3
+
4
+#include <mutex>
5
+#include <condition_variable>
6
+#include <thread>
7
+
8
+struct Manager {
9
+	Manager();
10
+	~Manager();
11
+
12
+	void WriteReport();
13
+	void DeployWorker();
14
+
15
+//	void work(std::unique_lock<x>);
16
+	
17
+    bool abort_reporting_;
18
+    std::mutex report_lock_;
19
+
20
+    std::condition_variable report_condition_;
21
+    std::thread report_thread_;
22
+	int data_to_protect_;
23
+};
24
+
25
+#endif
0 26