Skip to main content

Async

The Async class in the Tellusim API is a high-performance, multithreaded task scheduler designed for executing functions asynchronously across multiple CPU threads. It allows developers to dispatch tasks-functions with or without return values-either immediately or with a specific CPU affinity mask, enabling fine-grained control over thread usage. Internally, tasks are encapsulated using the AsyncFunction system, which supports various function signatures and handles function invocation and result storage. The Async class includes an inner Task class that represents individual asynchronous jobs. These tasks can be queued, cleared, canceled, or monitored for completion, and they offer mechanisms for retrieving return values in a type-safe way. This architecture is particularly suited for real-time or compute-intensive applications, such as rendering or simulation engines, where efficient CPU utilization and concurrent task execution are critical.

#include <core/TellusimAsync.h>

Constructors

Async()

Methods

Initialize threads.

bool init(uint32_t num = 0)

Shutdown threads.

bool shutdown()

Check status.

bool isInitialized() const

Number of threads.

static uint32_t getNumCores()
uint32_t getNumThreads() const

Temporary tasks.

void append(const Task &task)
void clear()

Number of temporary tasks.

uint32_t getNumTasks(bool check = false) const

Run function.

Task run(uint32_t mask)
Task run(uint64_t mask = ~0ull)
template<> Task run(const Func &func, uint64_t mask = ~0ull)
template<> Task run(const Func &func, A0 a0, uint64_t mask = ~0ull)
template<> Task run(const Func &func, A0 a0, A1 a1, uint64_t mask = ~0ull)
template<> Task run(const Func &func, A0 a0, A1 a1, A2 a2, uint64_t mask = ~0ull)
template<> Task run(const Func &func, A0 a0, A1 a1, A2 a2, A3 a3, uint64_t mask = ~0ull)
template<> Task run(const Func &func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4, uint64_t mask = ~0ull)
template<> Task run(const Func &func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, uint64_t mask = ~0ull)
template<> Task run(const Func &func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, uint64_t mask = ~0ull)
template<> Task run(const Func &func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, uint64_t mask = ~0ull)
template<> Task run(const Function<Func> &func, uint64_t mask = ~0ull)

Check completion status.

bool check(const Task *tasks, uint32_t num) const
bool check(Array<Task> tasks) const
bool check() const

Waiting for the completion.

bool wait(const Task *tasks, uint32_t num) const
bool wait(Array<Task> tasks) const
bool wait() const

Structs

Task

Task.

Constructors

Task()

Methods

Comparison operators.

bool operator==(const Task &task) const
bool operator!=(const Task &task) const

Task info.

bool empty() const
operator bool() const

Clear functions queue.

void clear()

Cancel functions queue.

void cancel()

Queue thread index.

uint32_t index()

Number of queued functions.

uint32_t size() const

Run the function on the queue.

template<> Task &run(const Func &func)
template<> Task &run(const Func &func, A0 a0)
template<> Task &run(const Func &func, A0 a0, A1 a1)
template<> Task &run(const Func &func, A0 a0, A1 a1, A2 a2)
template<> Task &run(const Func &func, A0 a0, A1 a1, A2 a2, A3 a3)
template<> Task &run(const Func &func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4)
template<> Task &run(const Func &func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5)
template<> Task &run(const Func &func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6)
template<> Task &run(const Func &func, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7)
template<> Task &run(const Function<Func> &func)

Check task completion status.

bool check(uint32_t num = Maxu32) const

Waiting for the task completion.

bool wait(uint32_t num = Maxu32) const

Function result.

template<> const Ret &get(uint32_t num = 0) const
template<> Ret getPtr(uint32_t num = 0) const
bool getBool(uint32_t num = 0) const
void run(AsyncFunctionBase *func)
void *get(uint32_t num) const