trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
ConcurrentTaskQueue.h
Go to the documentation of this file.
1
14
15#pragma once
16
18#include <trantor/exports.h>
19#include <list>
20#include <memory>
21#include <vector>
22#include <queue>
23#include <string>
24
25namespace trantor
26{
32class TRANTOR_EXPORT ConcurrentTaskQueue : public TaskQueue
33{
34 public:
41 ConcurrentTaskQueue(size_t threadNum, const std::string &name);
42
48 virtual void runTaskInQueue(const std::function<void()> &task);
49 virtual void runTaskInQueue(std::function<void()> &&task);
50
56 virtual std::string getName() const
57 {
58 return queueName_;
59 };
60
66 size_t getTaskCount();
67
72 void stop();
73
75
76 private:
77 size_t queueCount_;
78 std::string queueName_;
79
80 std::queue<std::function<void()>> taskQueue_;
81 std::vector<std::thread> threads_;
82
83 std::mutex taskMutex_;
84 std::condition_variable taskCond_;
85 std::atomic_bool stop_;
86 void queueFunc(int queueNum);
87};
88
89} // namespace trantor
size_t getTaskCount()
Get the number of tasks to be executed in the queue.
ConcurrentTaskQueue(size_t threadNum, const std::string &name)
Construct a new concurrent task queue instance.
void stop()
Stop all threads in the queue.
virtual std::string getName() const
Get the name of the queue.
Definition ConcurrentTaskQueue.h:56
virtual void runTaskInQueue(const std::function< void()> &task)
Run a task in the queue.
This class is a pure virtual class that can be implemented as a SerialTaskQueue or a ConcurrentTaskQu...
Definition TaskQueue.h:29
Definition EventLoop.h:34