You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.4 KiB
C++

12 months ago
/*
* Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
*
* This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#include <csignal>
#include <iostream>
#include "Util/logger.h"
#include "Util/util.h"
#include "Util/RingBuffer.h"
#include "Thread/threadgroup.h"
#include <list>
using namespace std;
using namespace toolkit;
8 months ago
//环形缓存写线程退出标记 [AUTO-TRANSLATED:ceeb4c96]
// Ring buffer write thread exit flag
12 months ago
bool g_bExitWrite = false;
8 months ago
//一个30个string对象的环形缓存 [AUTO-TRANSLATED:fee6a014]
// A ring buffer of 30 string objects
12 months ago
RingBuffer<string>::Ptr g_ringBuf(new RingBuffer<string>(30));
8 months ago
//写事件回调函数 [AUTO-TRANSLATED:19f6b6fa]
// Write event callback function
12 months ago
void onReadEvent(const string &str){
8 months ago
//读事件模式性 [AUTO-TRANSLATED:12cdd3a8]
// Read event mode
12 months ago
DebugL << str;
}
8 months ago
//环形缓存销毁事件 [AUTO-TRANSLATED:7da356eb]
// Ring buffer destruction event
12 months ago
void onDetachEvent(){
WarnL;
}
8 months ago
//写环形缓存任务 [AUTO-TRANSLATED:1467fea5]
// Write ring buffer task
12 months ago
void doWrite(){
int i = 0;
while(!g_bExitWrite){
8 months ago
//每隔100ms写一个数据到环形缓存 [AUTO-TRANSLATED:aedec620]
// Write data to the ring buffer every 100ms
12 months ago
g_ringBuf->write(to_string(++i),true);
usleep(100 * 1000);
}
}
int main() {
8 months ago
//初始化日志 [AUTO-TRANSLATED:371bb4e5]
// Initialize log
12 months ago
Logger::Instance().add(std::make_shared<ConsoleChannel>());
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
auto poller = EventPollerPool::Instance().getPoller();
RingBuffer<string>::RingReader::Ptr ringReader;
poller->sync([&](){
8 months ago
//从环形缓存获取一个读取器 [AUTO-TRANSLATED:c61b1c37]
// Get a reader from the ring buffer
12 months ago
ringReader = g_ringBuf->attach(poller);
8 months ago
//设置读取事件 [AUTO-TRANSLATED:6d9e7c68]
// Set read event
12 months ago
ringReader->setReadCB([](const string &pkt){
onReadEvent(pkt);
});
8 months ago
//设置环形缓存销毁事件 [AUTO-TRANSLATED:8fc24dc3]
// Set ring buffer destruction event
12 months ago
ringReader->setDetachCB([](){
onDetachEvent();
});
});
thread_group group;
8 months ago
//写线程 [AUTO-TRANSLATED:f91ceacd]
// Write thread
12 months ago
group.create_thread([](){
doWrite();
});
8 months ago
//测试3秒钟 [AUTO-TRANSLATED:942e7c08]
// Test for 3 seconds
12 months ago
sleep(3);
8 months ago
//通知写线程退出 [AUTO-TRANSLATED:b2b2a2af]
// Notify write thread to exit
12 months ago
g_bExitWrite = true;
8 months ago
//等待写线程退出 [AUTO-TRANSLATED:8150eaf3]
// Wait for write thread to exit
12 months ago
group.join_all();
8 months ago
//释放环形缓冲此时异步触发Detach事件 [AUTO-TRANSLATED:3aaaa2f4]
// Release ring buffer, triggering Detach event asynchronously
12 months ago
g_ringBuf.reset();
8 months ago
//等待异步触发Detach事件 [AUTO-TRANSLATED:a6ea0f45]
// Wait for asynchronous Detach event trigger
12 months ago
sleep(1);
8 months ago
//消除对EventPoller对象的引用 [AUTO-TRANSLATED:4f22453d]
// Remove reference to EventPoller object
12 months ago
ringReader.reset();
sleep(1);
return 0;
}