-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkItem2.h
More file actions
53 lines (39 loc) · 952 Bytes
/
WorkItem2.h
File metadata and controls
53 lines (39 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#ifndef _WORK_ITEM_H_
#define _WORK_ITEM_H_
#include <iostream>
#include <memory>
#include <chrono>
#include <atomic>
#include <thread>
class WorkItem {
public:
WorkItem() = default;
virtual ~WorkItem() = default;
virtual void Process() = 0;
virtual bool Ready() const = 0;
};
typedef std::shared_ptr<WorkItem> WorkItemPtr;
class SleepyWorkItem: public WorkItem {
public:
SleepyWorkItem(uint32_t id, uint32_t msec_duration):
id_(id), msec_duration_(msec_duration), ready_(false) {}
~SleepyWorkItem() = default;
void Process() override {
std::this_thread::sleep_for(std::chrono::milliseconds(msec_duration_));
ready_.store(true);
}
bool Ready() const override {
return ready_.load();
}
uint32_t id() const {
return id_;
}
uint32_t msec_duration() const {
return msec_duration_;
}
private:
uint32_t id_;
uint32_t msec_duration_;
std::atomic<bool> ready_;
};
#endif // _WORK_ITEM_H_