forked from yuvalif/rust2c-async-io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_async.cpp
More file actions
54 lines (42 loc) · 1.49 KB
/
example_async.cpp
File metadata and controls
54 lines (42 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <vector>
#include <string>
#include <chrono>
#include <future>
#include "async_file_hasher.h"
// Function to process a single file
std::string process_file_async(const std::string& filename) {
char* hash = calculate_md5_hash_sync_c(filename.c_str());
if (hash != nullptr) {
std::string result = filename + ": " + std::string(hash);
free_string(hash);
return result;
} else {
return filename + ": ERROR";
}
}
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <file1> [file2] [file3] ..." << std::endl;
return 1;
}
std::vector<std::string> files;
for (int i = 1; i < argc; i++) {
files.push_back(argv[i]);
}
std::cout << "\nProcessing " << files.size() << " files using C++20 async:" << std::endl;
auto start = std::chrono::high_resolution_clock::now();
// Launch async tasks for each file
std::vector<std::future<std::string>> futures;
for (const auto& file : files) {
futures.emplace_back(std::async(std::launch::async, process_file_async, file));
}
// Collect results
for (auto& future : futures) {
std::cout << future.get() << std::endl;
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
std::cout << "C++20 async processing time: " << duration.count() / 1000.0 << " ms" << std::endl;
return 0;
}