-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderedWorkerPool.h
More file actions
44 lines (34 loc) · 990 Bytes
/
OrderedWorkerPool.h
File metadata and controls
44 lines (34 loc) · 990 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
#ifndef _ORDERED_WORKER_POOL_H_
#define _ORDERED_WORKER_POOL_H_
#include <iostream>
#include <vector>
#include <thread>
#include <queue>
#include <mutex>
#include <condition_variable>
#include "WorkItem2.h"
class OrderedWorkerPool {
public:
typedef std::function<void (WorkItemPtr)> AppCallback;
OrderedWorkerPool(const AppCallback& callback, uint32_t pool_size);
~OrderedWorkerPool();
void Add(WorkItemPtr work_item);
private:
void DoWork();
void DoDelivery();
void ReadyForDelivery(WorkItemPtr work_item);
const AppCallback callback_;
// Consumer threads and their queue
std::queue<WorkItemPtr> queue_;
std::vector<std::thread> threads_;
std::mutex mutex_;
std::condition_variable cond_;
bool finished_;
// Delivery thread and its queue
std::thread delivery_thread_;
std::queue<WorkItemPtr> delivery_queue_;
std::mutex delivery_mutex_;
std::condition_variable delivery_cond_;
bool delivery_finished_;
};
#endif // _ORDERED_WORKER_POOL_H_