Condy v1.3.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 <iostream>
8#include <memory_resource>
9#include <vector>
10
11condy::pmr::Coro<void> task_func(auto &) { co_return; }
12
13condy::pmr::Coro<void> spawn_tasks(auto &alloc, size_t task_count) {
14 std::vector<condy::pmr::Task<void>> tasks;
15 tasks.reserve(task_count);
16
17 auto start = std::chrono::high_resolution_clock::now();
18
19 for (size_t i = 0; i < task_count; ++i) {
20 tasks.emplace_back(condy::co_spawn(task_func(alloc)));
21 }
22
23 for (auto &task : tasks) {
24 co_await std::move(task);
25 }
26
27 auto end = std::chrono::high_resolution_clock::now();
28 std::chrono::duration<double> duration = end - start;
29 double tasks_per_second =
30 static_cast<double>(task_count) / duration.count();
31 std::cout << "Spawned and completed " << task_count << " tasks in "
32 << duration.count() << " seconds (" << tasks_per_second
33 << " tasks/second)\n";
34}
35
36int main() noexcept(false) {
37 std::printf("This example is faster than benchmark spawn_tasks.cpp!!!\n");
38
39 const size_t task_count = 50'000'000;
40
41 std::pmr::monotonic_buffer_resource pool(1024l * 1024 * 100); // 100 MB pool
42 std::pmr::polymorphic_allocator<std::byte> allocator(&pool);
43
44 condy::sync_wait(spawn_tasks(allocator, task_count));
45
46 return 0;
47}
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:240