LogSystem beta 1.4
This is a logging system project
 
载入中...
搜索中...
未找到
looper.hpp
浏览该文件的文档.
1
7#pragma once
8#include "buffer.hpp"
9#include <mutex>
10#include <condition_variable>
11#include <thread>
12#include <functional>
13#include <memory>
14#include <atomic>
15
16namespace Xulog
17{
24 using Functor = std::function<void(Buffer &)>;
33 enum class AsyncType
34 {
37 };
45 {
46 public:
47 using ptr = std::shared_ptr<AsyncLooper>;
57 : _stop(false), _thread(std::thread(&AsyncLooper::threadEntry, this)), _looper_type(asynctype), _callBack(func)
58 {
59 }
66 {
67 stop();
68 _cond_con.notify_all(); // 唤醒所有工作线程
69 _thread.join(); // 等待线程
70 }
76 void stop()
77 {
78 _stop = true;
79 _cond_con.notify_all(); // 唤醒所有的工作线程
80 }
89 void push(const char *data, size_t len)
90 {
91 std::unique_lock<std::mutex> lock(_mutex);
92 // 条件变量控制,若缓冲区剩余大于等于数据长度,则返回真
94 {
95 _cond_pro.wait(lock, [&]()
96 { return _pro_buf.writeAbleSize() >= len; });
97 }
98 // 满足条件,添加数据
99 _pro_buf.push(data, len);
100 // 唤醒消费者对缓冲区的数据进行处理
101 _cond_con.notify_one();
102 }
103
104 private:
112 void threadEntry() // 线程入口函数
113 {
114 while (true)
115 {
116 // 判断生产缓冲区是否有数据,有则交换,无则阻塞
117 {
118 std::unique_lock<std::mutex> lock(_mutex);
119 if (_stop && _pro_buf.empty())
120 break;
121 _cond_con.wait(lock, [&]()
122 { return _stop || !_pro_buf.empty(); });
123
125 }
126 // 处理消费缓冲区
128 // 初始化消费缓冲区
129 _con_buf.reset();
130 // 唤醒生产者
132 {
133 _cond_pro.notify_all();
134 }
135 }
136 }
137
138 private:
139 std::atomic<bool> _stop;
142 std::mutex _mutex;
143 std::condition_variable _cond_pro;
144 std::condition_variable _cond_con;
145 std::thread _thread;
148 };
149}
实现异步日志缓冲区
异步工作器类
Definition looper.hpp:45
std::atomic< bool > _stop
停止标志
Definition looper.hpp:139
std::mutex _mutex
互斥锁
Definition looper.hpp:142
std::shared_ptr< AsyncLooper > ptr
Definition looper.hpp:47
Buffer _pro_buf
生产缓冲区
Definition looper.hpp:140
AsyncLooper(const Functor &func, AsyncType asynctype=AsyncType::ASYNC_SAFE)
构造函数
Definition looper.hpp:56
std::condition_variable _cond_con
消费者条件变量
Definition looper.hpp:144
void push(const char *data, size_t len)
向生产缓冲区推送数据
Definition looper.hpp:89
AsyncType _looper_type
异步类型
Definition looper.hpp:146
void threadEntry()
线程入口函数
Definition looper.hpp:112
Buffer _con_buf
消费缓冲区
Definition looper.hpp:141
~AsyncLooper()
析构函数
Definition looper.hpp:65
void stop()
停止异步工作器
Definition looper.hpp:76
std::condition_variable _cond_pro
生产者条件变量
Definition looper.hpp:143
std::thread _thread
异步工作器的线程
Definition looper.hpp:145
Functor _callBack
回调函数
Definition looper.hpp:147
异步日志缓冲区类
Definition buffer.hpp:24
void push(const char *data, size_t len)
向缓冲区写入数据
Definition buffer.hpp:43
bool empty()
判空
Definition buffer.hpp:121
size_t writeAbleSize()
获取可写数据的长度
Definition buffer.hpp:76
void swap(Buffer &buffer)
交换当前缓冲区与另一个缓冲区
Definition buffer.hpp:110
void reset()
重置缓冲区
Definition buffer.hpp:98
Definition buffer.hpp:12
AsyncType
异步工作器类型
Definition looper.hpp:34
@ ASYNC_UNSAFE
不考虑资源,无限扩容,性能测试
@ ASYNC_SAFE
缓冲区满则阻塞
std::function< void(Buffer &)> Functor
回调函数类型
Definition looper.hpp:24