Condy v1.5.0
C++ Asynchronous System Call Layer for Linux
Loading...
Searching...
No Matches
custom-allocator.cpp
Go to the documentation of this file.
1
5
6#include <condy.hpp>
7#include <format>
8#include <iostream>
9#include <memory_resource>
10#include <vector>
11
12condy::pmr::Coro<void> task_func(auto &) { co_return; }
13
14condy::pmr::Coro<void> spawn_tasks(auto &alloc, size_t task_count) {
15 std::vector<condy::pmr::Task<void>> tasks;
16 tasks.reserve(task_count);
17
18 auto start = std::chrono::high_resolution_clock::now();
19
20 for (size_t i = 0; i < task_count; ++i) {
21 tasks.emplace_back(condy::co_spawn(task_func(alloc)));
22 }
23
24 for (auto &task : tasks) {
25 co_await std::move(task);
26 }
27
28 auto end = std::chrono::high_resolution_clock::now();
29 std::chrono::duration<double> duration = end - start;
30 double tasks_per_second =
31 static_cast<double>(task_count) / duration.count();
32 std::cout << std::format("Spawned and completed {} tasks in {:.2f} seconds "
33 "({:.2f} tasks/second)\n",
34 task_count, duration.count(), tasks_per_second);
35}
36
37int main() noexcept(false) {
38 std::cout << "This example is faster than benchmark spawn_tasks.cpp!!!\n";
39
40 const size_t task_count = 50'000'000;
41
42 std::pmr::monotonic_buffer_resource pool(1024l * 1024 * 100); // 100 MB pool
43 std::pmr::polymorphic_allocator<std::byte> allocator(&pool);
44
45 condy::sync_wait(spawn_tasks(allocator, task_count));
46
47 return 0;
48}
Main include file for the Condy library.
condy::Coro< T, std::pmr::polymorphic_allocator< std::byte > > Coro
Coroutine type using polymorphic allocator.
Definition pmr.hpp:26
T sync_wait(Runtime &runtime, Coro< T, Allocator > coro)
Synchronously wait for a coroutine to complete in the given runtime.
Definition sync_wait.hpp:24
Task< T, Allocator > co_spawn(Runtime &runtime, Coro< T, Allocator > coro)
Spawn a coroutine as a task in the given runtime.
Definition task.hpp:266