-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtask_queue.h
More file actions
49 lines (41 loc) · 993 Bytes
/
task_queue.h
File metadata and controls
49 lines (41 loc) · 993 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
/**
* @Description :
* @Author : chenht2022
* @Date : 2024-07-16 10:43:18
* @Version : 1.0.0
* @LastEditors : chenht
* @LastEditTime : 2024-10-09 11:08:07
* @Copyright (c) 2024 by KVCache.AI, All Rights Reserved.
**/
#ifndef CPUINFER_TASKQUEUE_H
#define CPUINFER_TASKQUEUE_H
#include <atomic>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <queue>
#include <thread>
#include <vector>
class TaskQueue {
public:
TaskQueue();
~TaskQueue();
void enqueue(std::function<void()>);
void sync(size_t allow_n_pending);
private:
struct Node {
std::function<void()> task;
std::atomic<Node*> next;
Node() : task(nullptr), next(nullptr) {}
Node(const std::function<void()>& t) : task(t), next(nullptr) {}
};
std::atomic<Node*> head;
std::atomic<Node*> tail;
std::atomic<bool> done;
std::atomic<size_t> pending;
std::thread workerThread;
std::mutex mtx;
std::condition_variable cv;
void worker();
};
#endif