ladivic
Loading...
Searching...
No Matches
ldvc_async.hpp File Reference

Provides utilities for asynchronous execution in C++. More...

#include <future>
Include dependency graph for ldvc_async.hpp:

Go to the source code of this file.

Functions

template<typename F , typename... A>
std::future< typename std::result_of< F(A...)>::typeldvc_async_execute (F &&f, A &&... args)
 Executes a function asynchronously.
 
template<typename R , typename P , typename F , typename... A>
std::future< typename std::result_of< F(A...)>::typeldvc_async_execute_with_delay (const std::chrono::duration< R, P > &delay, F &&f, A &&... args)
 Executes a function asynchronously with a delay.
 
template<typename R , typename P , typename F , typename... A>
std::future< typename std::result_of< F(A...)>::typeldvc_async_execute_with_timeout (const std::chrono::duration< R, P > &timeout, F &&f, A &&... args)
 Executes a function asynchronously with a timeout.
 

Detailed Description

Provides utilities for asynchronous execution in C++.

This header file defines functions for executing tasks asynchronously in C++, providing capabilities for delayed execution and timeout handling.

Author
Nathanne Isip

Definition in file ldvc_async.hpp.

Function Documentation

◆ ldvc_async_execute()

template<typename F , typename... A>
std::future< typename std::result_of< F(A...)>::type > ldvc_async_execute ( F && f,
A &&... args )

Executes a function asynchronously.

This function executes the given function f asynchronously, along with its arguments args. It returns a std::future object representing the result of the function call.

Template Parameters
FThe type of the function to be executed.
AThe types of the arguments to the function.
Parameters
fThe function to be executed.
argsThe arguments to the function.
Returns
std::future<typename std::result_of<F(A...)>::type> A future object representing the result of the function call.

Definition at line 60 of file ldvc_async.hpp.

61{
62 using return_type = typename std::result_of<F(A...)>::type;
63
64 std::packaged_task<return_type()> task(std::bind(std::forward<F>(f), std::forward<A>(args)...));
65 std::future<return_type> result = task.get_future();
66 std::thread(std::move(task)).detach();
67
68 return result;
69}
T * ldvc_malloc(usize size)
Allocates memory for an array of elements.
Definition ldvc_mem.hpp:61

◆ ldvc_async_execute_with_delay()

template<typename R , typename P , typename F , typename... A>
std::future< typename std::result_of< F(A...)>::type > ldvc_async_execute_with_delay ( const std::chrono::duration< R, P > & delay,
F && f,
A &&... args )

Executes a function asynchronously with a delay.

This function executes the given function f asynchronously, along with its arguments args, after the specified delay. It returns a std::future object representing the result of the function call.

Template Parameters
RThe type of the delay duration.
PThe type of the delay duration.
FThe type of the function to be executed.
AThe types of the arguments to the function.
Parameters
delayThe delay duration before executing the function.
fThe function to be executed.
argsThe arguments to the function.
Returns
std::future<typename std::result_of<F(A...)>::type> A future object representing the result of the function call.

Definition at line 93 of file ldvc_async.hpp.

98{
99 return std::async(std::launch::async, [=]() {
100 std::this_thread::sleep_for(delay);
101 return f(std::forward<A>(args)...);
102 });
103}

◆ ldvc_async_execute_with_timeout()

template<typename R , typename P , typename F , typename... A>
std::future< typename std::result_of< F(A...)>::type > ldvc_async_execute_with_timeout ( const std::chrono::duration< R, P > & timeout,
F && f,
A &&... args )

Executes a function asynchronously with a timeout.

This function executes the given function f asynchronously, along with its arguments args, and provides a timeout mechanism. If the function does not complete within the specified timeout duration, an exception is thrown.

Template Parameters
RThe type of the timeout duration.
PThe type of the timeout duration.
FThe type of the function to be executed.
AThe types of the arguments to the function.
Parameters
timeoutThe timeout duration for the function execution.
fThe function to be executed.
argsThe arguments to the function.
Returns
std::future<typename std::result_of<F(A...)>::type> A future object representing the result of the function call.
Exceptions
std::runtime_errorThrown if the function execution exceeds the specified timeout duration.

Definition at line 130 of file ldvc_async.hpp.

135{
136 std::promise<typename std::result_of<F(A...)>::type> promise;
137 std::future<typename std::result_of<F(A...)>::type> result = promise.get_future();
138
139 std::thread([=, &promise]() {
140 try {
141 f(std::forward<A>(args)...);
142 } catch (...) {
143 promise.set_exception(std::current_exception());
144 return;
145 }
146 promise.set_value();
147 }).detach();
148
149 std::thread([=, &promise]() {
150 std::this_thread::sleep_for(timeout);
151 promise.set_exception(std::make_exception_ptr(std::runtime_error("Timeout")));
152 }).detach();
153
154 return result;
155}