Skip to content

Latest commit

 

History

History
64 lines (52 loc) · 1.59 KB

0.手写多线程回调地狱callback-hell.md

File metadata and controls

64 lines (52 loc) · 1.59 KB
#include <chrono>
#include <fstream>
#include <functional>
#include <iostream>
#include <string>
#include <thread>

using namespace std;

// 异步读取文件
void async_read_file(const string &filename,
                     function<void(const string &)> callback) {
  thread t([filename, callback]() {
    // 模拟读取文件需要一段时间
    this_thread::sleep_for(chrono::seconds(1));

    ifstream ifs(filename);
    string content((istreambuf_iterator<char>(ifs)),
                   (istreambuf_iterator<char>()));
    callback(content);
  });
  t.detach();
}

// 异步写入文件
void async_write_file(const string &filename, const string &content,
                      function<void()> callback) {
  thread t([filename, content, callback]() {
    // 模拟写入文件需要一段时间
    this_thread::sleep_for(chrono::seconds(1));

    ofstream ofs(filename);
    ofs << content;
    callback();
  });
  t.detach();
}

// 读取文件 -> 处理数据 -> 写入文件
void process_file(const string &input_filename, const string &output_filename) {
  async_read_file(input_filename, [output_filename](const string &content) {
    // 处理数据
    string processed_content = content + "processed";

    async_write_file(output_filename, processed_content,
                     []() { cout << "File written successfully!" << endl; });
  });
}

int main() {
  process_file("input.txt", "output.txt");

  // 主线程继续执行其他任务
  for (int i = 0; i < 5; i++) {
    cout << "Main thread is working..." << endl;
    this_thread::sleep_for(chrono::seconds(1));
  }

  return 0;
}