线程通信

// 用于处理日志行的函数 void aabb::process_log_line(const std::string& line) { std::smatch match; std::regex pattern("current iteration:(\\d+)"); // 匹配迭代次数的正则表达式 ..... } // 读取并处理日志文件的函数(在单独的线程中运行) void aabb::read_log_file(const std::string& log_file_path) { std::ifstream file(log_file_path); if (!file.is_open()) { std::cerr << "Failed to open log file: " << log_file_path << std::endl; return; } std::string line; while (!stop_reading && std::getline(file, line)) { process_log_line(line); // 处理日志行 } // 等待所有日志行都被处理(虽然在这个例子中可能不需要) // cv.wait(lock, []{ /* 这里可以放置一个条件检查 */ }); } aabb::run() { std::string log_file_path = "output.log"; // 替换为你的日志文件路径 // 在单独的线程中启动日志文件读取器 std::thread log_reader_thread(read_log_file, log_file_path); // 这里模拟你的长时间运行任务(例如,调用外部EXE) // 注意:在实际应用中,你会使用system()或其他方式来启动你的EXE // 这里只是用一个简单的循环来模拟 for (int i = 0; i < 10; ++i) { std::this_thread::sleep_for(std::chrono::seconds(5)); // 假设每次迭代需要5秒 // 在这里,你可以模拟向日志文件中写入"current iteration:xxx"的行 // ... } // 当任务完成后,设置标志位以停止读取日志文件 { std::lock_guard<std::mutex> lock(mtx); stop_reading = true; } // cv.notify_all(); // 通知所有等待的线程(虽然在这个例子中我们可能没有线程在等待) // 等待读取日志文件的线程完成 log_reader_thread.join(); return 0; }其中 read_log_file那边报错

在您的代码中,read_log_file 和 process_log_line 是 aabb 类的成员函数,但是 stop_reading 和 mtx 被声明为全局变量,而它们实际上应该作为 aabb 类的成员变量,以便在类的不同函数之间共享状态。

此外,run 函数被声明为 aabb 类的一个成员函数,但通常我们不会在类内部直接声明一个名为 run 的成员函数作为程序的入口点(除非您正在编写一个特定的框架或库,如多线程服务器)。在C++中,程序的入口点是 main 函数。

下面是一个修改后的示例,将 stop_reading 和 mtx 作为 aabb 类的成员变量,并创建了一个 main 函数作为程序的入口点:

 

cpp复制代码

#include <iostream>
#include <fstream>
#include <thread>
#include <mutex>
#include <regex>
class aabb {
private:
std::mutex mtx; // 互斥锁,用于线程安全
bool stop_reading = false; // 停止读取的标志位
// 用于处理日志行的函数
void process_log_line(const std::string& line) {
std::smatch match;
std::regex pattern("current iteration:(\\d+)"); // 匹配迭代次数的正则表达式
// ... 省略其他实现 ...
}
// 读取并处理日志文件的函数(在单独的线程中运行)
void read_log_file(const std::string& log_file_path) {
std::ifstream file(log_file_path);
if (!file.is_open()) {
std::cerr << "Failed to open log file: " << log_file_path << std::endl;
return;
}
std::string line;
while (!stop_reading && std::getline(file, line)) {
std::lock_guard<std::mutex> lock(mtx); // 锁定互斥锁以安全访问共享资源
process_log_line(line); // 处理日志行
}
}
public:
// 这里可能还有其他成员函数和变量
// 模拟程序的运行(非真实的run方法,只是示例)
void start() {
std::string log_file_path = "output.log"; // 替换为你的日志文件路径
// 在单独的线程中启动日志文件读取器
std::thread log_reader_thread(&aabb::read_log_file, this, log_file_path);
// 这里模拟你的长时间运行任务(例如,调用外部EXE)
// 注意:在实际应用中,你会使用system()或其他方式来启动你的EXE
// 这里只是用一个简单的循环来模拟
for (int i = 0; i < 10; ++i) {
std::this_thread::sleep_for(std::chrono::seconds(5)); // 假设每次迭代需要5秒
// 在这里,你可以模拟向日志文件中写入"current iteration:xxx"的行
// ...
}
// 当任务完成后,设置标志位以停止读取日志文件
{
std::lock_guard<std::mutex> lock(mtx);
stop_reading = true;
}
// 等待读取日志文件的线程完成
log_reader_thread.join();
}
};
int main() {
aabb my_object; // 创建aabb类的实例
my_object.start(); // 调用start方法来启动程序
return 0;
}

请注意,我添加了 start 方法来模拟程序的运行,而不是使用 run。在真实的应用程序中,您可能需要根据实际的需求来设计类和方法。此外,我还将 std::lock_guard<std::mutex> 的使用移到了 read_log_file 函数中,以确保在调用 process_log_line 之前锁定互斥锁。这取决于 process_log_line 是否需要访问共享资源,如果不需要,则不需要在每次调用时都锁定互斥锁。

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江河地笑

实践是检验真理的唯一标准

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值