Ai
1 Star 0 Fork 0

ALONE_WORK/mqct

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.cpp 3.12 KB
一键复制 编辑 原始数据 按行查看 历史
lijiangjun 提交于 2025-06-06 09:23 +08:00 . 增加 example11 编译配置文件。
/*
日期:2025-06-05
作者:lijj
作用:一个发送者,多接收者处理耗时测试[post]
*/
#include <iostream>
#include <string>
#if defined(_WIN32)
#include <Windows.h>
#include <process.h>
#else
#include <unistd.h>
#endif
#include <chrono>
#include "mgnotify.h"
#include "center.hpp"
using namespace std;
using namespace chrono;
typedef std::chrono::high_resolution_clock::time_point chpt;
class carrier : public BaseMessage<carrier>
{
public:
string name;
// 测试耗时: 开始时间
chpt start_time;
};
class recv1 : public BaseHandle<carrier>
{
public:
recv1() : _id(-1) {}
~recv1() {}
void setId(int id) { _id = id; }
virtual bool handle(const carrier* msg) override;
private:
int _id;
};
class sender
{
public:
sender() {}
~sender() {}
void send() {
carrier m1;
m1.start_time = high_resolution_clock::now();
m1.name = "ljj";
m1.post();
}
};
void logo()
{
cout << endl;
cout << endl;
cout << " _________ __ \n";
cout << " / _____// |_ ____ ____ ____ \n";
cout << " \\_____ \\\\ __\\/ _ \\ / \\_/ __ \\ \n";
cout << " / \\| | ( <_> ) | \\ ___/ \n";
cout << "/_______ /|__| \\____/|___| /\\___ > \n";
cout << " \\/ \\/ \\/ \n";
cout << endl;
cout << endl;
}
void title()
{
cout << endl;
cout << " 日期:2025-06-05 \n";
cout << " 作者:lijj \n";
cout << " 作用:个发送者,多接收者接收消息测试[post] \n";
cout << endl;
}
void enter()
{
logo();
title();
while(true) {
string in;
cin >> in;
cout << "enter: " << in << endl;
if(in == "quit")
break;
}
}
int main()
{
#if defined(_WIN32 )
SetConsoleOutputCP(65001); // UTF-8
SetConsoleCP(65001); // 输入也设为UTF-8
#endif
thread t(enter);
list<recv1*> rs1;
auto start = high_resolution_clock::now();
int count = 10000;
cout << count << "个接收者构建中,请稍后...\n";
for(int i = 0; i < count; i++) {
recv1 *r1 = new recv1;
r1->setId(i);
rs1.push_back(r1);
}
auto end = high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed = end - start;
cout << "attach " << count << " 接收者耗时:" << elapsed.count() << " ms." << endl;
sender s1;
s1.send();
t.join();
start = high_resolution_clock::now();
while (!rs1.empty()) {
auto t = rs1.back();
rs1.pop_back();
delete t;
t = nullptr;
}
end = high_resolution_clock::now();
elapsed = end - start;
cout << "detach " << count << " 接收者耗时:" << elapsed.count() << " ms." << endl;
return 0;
}
bool recv1::handle(const carrier* msg)
{
auto end = high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed = end - msg->start_time;
cout << "recv1--> id: " << _id << " name: " << msg->name << ", 从开始发送消息到接收消息耗时: " << elapsed.count() << " ms." << endl;
return true;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/ALONE_WORK/mqct.git
git@gitee.com:ALONE_WORK/mqct.git
ALONE_WORK
mqct
mqct
master

搜索帮助