Skip to content

Commit a25ca96

Browse files
committed
add another async function
1 parent 72c9059 commit a25ca96

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎example/main.cpp‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
#include <random>
22

3+
#include <nexus/async.h>
34
#include <nexus/pool.h>
45

6+
void asyncFunction(int i) {
7+
for (int i = 0; i < 100000000; i++) {
8+
continue;
9+
}
10+
logger::warning("Async task " + std::to_string(i) + " finished");
11+
}
12+
513
int main() {
614
nexus::pool pool(8);
715

816
std::vector<std::future<std::string>> results;
917

18+
for (int i = 0; i < 500; ++i) {
19+
nexus::async(asyncFunction, i);
20+
}
21+
1022
for (int i = 0; i < 500; ++i) {
1123
results.emplace_back(pool.enqueue([i] {
1224
for (int j = 0; j < 100000000; j++) {
@@ -21,5 +33,5 @@ int main() {
2133
logger::info("Result: " + result.get());
2234
}
2335

24-
return 0;
36+
return EXIT_SUCCESS;
2537
}

‎include/nexus/async.h‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include <functional>
4+
#include <thread>
5+
#include <utility>
6+
7+
#include <logger/log.h>
8+
9+
namespace nexus {
10+
11+
template <class F, class... Args> void async(F &&f, Args &&...args) {
12+
try {
13+
std::thread(std::forward<F>(f), std::forward<Args>(args)...).detach();
14+
} catch (const std::exception &e) {
15+
logger::error(
16+
e.what(),
17+
"template <class F, class... Args> void async(F &&f, Args &&...args)");
18+
}
19+
}
20+
21+
} // namespace nexus

0 commit comments

Comments
 (0)