Logo
blank Skip to main content

Developing Reactive Applications: Asynchronous Programming in Rust vs Coroutines in C++

C++

End users hate waiting for a response from an application. But some operations canโ€™t be started until another task is complete. To address this problem, modern networking applications rely heavily on asynchronous execution of interdependent tasks. The aim is to optimize the performance of high-load servers and improve the reactiveness of networking applications.

In Rust and C++20, programmers use different approaches when building such applications: asynchronous programming and coroutines. In this article, we compare ways of implementing Rust async await vs C++ coroutines and provide examples based on dedicated libraries โ€” Tokio for Rust and Boost.Asio for C++20.

This article will be useful for project managers and development teams considering different options for building a networking application that requires asynchronous task processing.

Initially, programs were written and executed synchronously, with every line of code being executed in strict order, one after another. In this approach, a task canโ€™t be started until the previous task has finished. This can lead to significant performance issues, especially in complex solutions with multiple interdependent operations.

As an alternative to synchronous programming, we have asynchronous programming and coroutines. These mechanisms allow processes to progress without necessarily waiting for the completion of previous tasks.

synchronous programming vs asynchronous programming

Asynchronous programming and coroutines are now the de facto standard for developing reactive applications that work with networks or file systems.

They optimize high-load programs that frequently wait for tasks to be completed (e.g. waiting for a response from a client) and allow other tasks to be performed without waiting for the completion of the primary task. Additionally, they provide a solution to the 10K connections problem, which is critical for high-load servers.

Below, we analyze current approaches to asynchronous operation handling in two popular programming languages โ€” Rust and C++20.

Looking to build an advanced solution? 

Discover how Aprioritโ€™s niche expertise can help you bring even the most ambitious idea to life!

Asynchronous programming in Rust

Since 2018, Rust programmers have had a built-in solution for asynchronous programming through the Future trait, which represents an async task and its interface. Now, asynchronous operations in Rust rely heavily on the Future trait and the types that implement it.

The Future trait represents data that is currently unavailable but can be obtained at some point in the future. This trait allows for reading from a TCP connection or any operation that takes a long time to complete. 

The Future trait has a poll function, which you can call in order for the Future trait to make further progress.

Hereโ€™s the definition of the Future trait:

Rust
pub trait Future { 
โ€ฏโ€ฏโ€ฏ type Output; 
 
โ€ฏโ€ฏโ€ฏ fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>; 
}

Letโ€™s look closer at the types and mechanisms that you need to implement the Future trait:

  • Output โ€” This is the type that the operation task resolves to. For a TCP reading, the Output type would be Result<usize, io::Error>. Upon successful completion of the task, you will receive Ok(usize) with the number of read bytes. If an error occurs, you will receive the Err(io::Error) result.
  • Pinning โ€” This is a mechanism for attaching data to a specific location in memory. By using Pin, you guarantee that the object wonโ€™t be moved to another location in memory. For example, Pin<&mut Self> indicates that &mut Self will always be in the same memory location between the calls to Self::poll. Using the same memory location is mandatory if you want to be sure that the fields of Self donโ€™t change their memory address.
  • Context<‘_> โ€” This is a wrapper type around the Waker handle. Waker informs the calling code that the task can progress, for example, when the needed file becomes available for reading.

A task can be in progress or completed. The state of a task is shown in the return value of the poll function.

Rust
pub enum Poll<T> { 
โ€ฏโ€ฏโ€ฏ Ready(T), 
โ€ฏโ€ฏโ€ฏ Pending, 
}

If after calling the poll function you receive Poll::Pending, the task is still in progress. If you receive Poll::Ready(T), the task has been completed.

Letโ€™s take a look at a practical example of creating a simple timer with the Future trait and polling:

Rust
use std::future::Future; 
use std::pin::Pin; 
use std::sync::Arc; 
use std::task::{Context, Poll, Wake, Waker}; 
use std::time::Duration; 
use std::{io, time}; 
use time::Instant; 
 
struct MyWaker {} 
 
impl Wake for MyWaker { 
โ€ฏ โ€ฏ fn wake(self: Arc<Self>) {} 
} 
 
 
struct Timer { 
โ€ฏ โ€ฏ start: Instant, 
โ€ฏ โ€ฏ wait: u32, 
} 
 
 
impl Timer { 
โ€ฏ โ€ฏ fn new(wait: u32) -> Timer { 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ Timer { 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ start: Instant::now(), 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ wait, 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ } 
โ€ฏ โ€ฏ } 
โ€ฏ โ€ฏ fn wait(&mut self) { 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ let waker = Waker::from(Arc::new(MyWaker {})); 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ let mut context = Context::from_waker(&waker); 
 
 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ loop { 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ match Pin::new(&mut *self).poll(&mut context) { 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ Poll::Ready(elapsed) => { 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ println!("timer finished: elapsed {elapsed:?} secs"); 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ break; 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ } 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ Poll::Pending => {  // task is not ready for now, so sleep 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ std::thread::sleep(Duration::from_secs(1)); 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ } 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ } 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ } 
โ€ฏ โ€ฏ } 
} 
 
 
impl Future for Timer { 
โ€ฏ โ€ฏ type Output = u32; 
 
 
โ€ฏ โ€ฏ fn poll(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> { 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ let time_point = Instant::now(); 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ let elapsed = time_point.saturating_duration_since(self.start).as_secs() as u32; 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ return if elapsed > self.wait { 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ Poll::Ready(elapsed) 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ } else { 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏ Poll::Pending 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ }; 
โ€ฏ โ€ฏ } 
} 
 
 
fn main() { 
โ€ฏ โ€ฏ let mut timer = Timer::new(5); 
โ€ฏ โ€ฏ timer.wait(); 
}

Here, we have a Timer structure that contains the starting time and the number of seconds to wait. In the main function, we create a Timer instance that waits for five seconds and then calls Timer::wait.

In Timer::wait, we create a MyWaker handle that implements the Wake trait. MyWaker is the simplest Waker from which we create Context.

Next, we have a loop that polls our Timer structure. Pin::new(&mut *self) creates a Pin<&mut Self> to call Future::poll.

When we call Timer::poll, it can return one of the two results:

  • Poll::Ready โ€” This result shows that the specified time interval has passed. After receiving Poll::Ready, we can output the result to the console and exit.
  • Poll::Pending โ€” This result shows that the required time interval hasnโ€™t passed yet. In this case, we go back to polling after a one-second pause.

Thus, implementing the Future trait for the Timer structure is quite simple โ€” if the required time interval has passed, we return Poll::Ready with the number of seconds that have passed, and if the interval hasnโ€™t passed yet, we return Poll::Pending.

Result of Future trait execution for the Timer structure
Screenshot 1. Result of Future trait execution for the Timer structure

Now, letโ€™s look closer at the specifics of the Async/Await syntax in Rust.

Read also

Rust vs C++ Comparison

Choose a suitable programming language for your software with our comprehensive comparison between Rust and C! Gain valuable insights into their features, performance, and suitability for different projects. 

Learn more

Async/Await syntax in Rust

The Poll mechanism we analyzed above is considered to be low-level. Therefore, while itโ€™s useful to know the basics of this mechanism, youโ€™re unlikely to use it or implement the Future trait manually when building a Rust solution.

To simplify the use of the Future trait, Rust developers use Async/Await syntax, which allows them to write asynchronous functions and blocks and wait for their execution.

For example, we can create a write function:

Rust
async fn write(stream: &mut TcpStream,  bytes: &[u8]) -> io::Result<u32>

To execute this function, we use the .await syntax:

Rust
let mut stream = TcpStream::connect(โ€œ127.0.0.1:8080โ€).unwrap();  
async { 
	
let count = write(&mut stream, โ€œHello thereโ€.as_bytes()).await; 
	println!(โ€œwrote {count} bytesโ€); 
}

As you can see, this code is easier to write and read compared to code that uses the poll function.
The compiler rewrites async functions into functions that return the Future trait. For example, the compiler will rewrite the write function from the previous code sample to the following:

Rust
fn write(stream: &mut TcpStream, bytes: &[u8]) -> imp Future<type Output=io::Result<u32>>

The .await syntax works with any function that returns impl Future. For example, with the Timer structure, instead of polling the wait function, we can do the following:

Rust
  let mut timer = Timer::new(5); 
async { 
โ€ฏ โ€ฏ โ€ฏlet elapsed = timer.await; 
โ€ฏ โ€ฏ โ€ฏprintln!("timer finished: elapsed {elapsed:?} secs"); 
โ€ฏ}

We can also gather several Futures and execute them:

Rust
let timers = vec![Timer::new(5), Timer::new(10), Timer::new(15)]; 
for timer in timers { 
โ€ฏ โ€ฏ async { 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏlet elapsed = timer.await; 
โ€ฏ โ€ฏ โ€ฏ โ€ฏ โ€ฏprintln!("timer finished: elapsed {elapsed:?} secs"); 
โ€ฏ โ€ฏ }; 
}

Yet this code wonโ€™t compile. Future will only start execution after the first .await. We can apply the await mechanism only when Future is in an async block or async function. However, we also must apply the await mechanism to the async block, as it also resolves to imp Future.

To address this problem, we need to apply the Runtime mechanism for the Future trait. This mechanism will execute async Futures in synchronous code.

The tricky thing is that the Runtime mechanism is unavailable in the standard library. Consequently, we have two alternatives for implementing this mechanism:

  • Write a custom Runtime mechanism
  • Use a library that provides async Runtime (such as Tokio or async-std)

In this article, we overview the use of the Tokio library for implementing the Runtime mechanism for Rust coroutines.

Working with the Tokio library

The Tokio library provides a Runtime for executing Futures in Rust. Using this library, we can add async variants of standard library primitives such as TcpListener, TcpStream, File, and so on.

Hereโ€™s how to add Tokio to a project:

Rust
cargo add tokio -F full 

And this is what the Timer structure implemented with this library looks like:

Rust
fn main() { 
โ€ฏโ€ฏโ€ฏ let runtime = tokio::runtime::Builder::new_multi_thread().build().unwrap(); 
โ€ฏโ€ฏโ€ฏ runtime.block_on(async { 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ let timer = Timer::new(5); 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ let elapsed = timer.await; 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ println!("timer finished: elapsed {elapsed:?} secs"); 
โ€ฏโ€ฏโ€ฏ }); 
}

In this example, we create a Tokio Runtime that polls the Futures running on it. In particular, we create a multi-threaded runtime, which provides us with eight system threads to run our Futures on. Itโ€™s also possible to create a single-threaded runtime where our Futures will be executed on a single system thread.

Next, using the Runtime::block_on method, we launch a Future on the Runtime. This method is responsible for blocking execution of the main thread until the Future passed to it completes its work. Developers use this method to start the main Future, which will then start all the necessary sub-Futures.We also need to add a Waker waking mechanism to the Future trait implementation for the Timer structure. In this way, the Tokio executor will know that the task needs to be polled again because it hasnโ€™t finished.

Rust
impl Future for Timer { 
โ€ฏโ€ฏโ€ฏ type Output = u32; 
โ€ฏ 
โ€ฏโ€ฏโ€ฏ fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ let time_point = Instant::now(); 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ let elapsed = time_point.saturating_duration_since(self.start).as_secs() as u32; 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ return if elapsed > self.wait { 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ Poll::Ready(elapsed) 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ } else { 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ let waker = cx.waker(); 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ waker.wake_by_ref(); 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ Poll::Pending 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ }; 
โ€ฏโ€ฏโ€ฏ } 
}

After running this code, we receive the following results:

Timer execution
Screenshot 2. Result of Timer execution

Note that we donโ€™t have to always create a Tokio Runtime manually. Instead, we can use the #[tokio::main] macro. 

This is what the main function from earlier would look like if we rewrite it with this macro:

Rust
#[tokio::main] 
async fn main() { 
โ€ฏโ€ฏโ€ฏ let timer = Timer::new(5); 
โ€ฏโ€ฏโ€ฏ let elapsed = timer.await; 
โ€ฏโ€ฏโ€ฏ println!("timer finished: elapsed {elapsed:?} secs"); 
}

Letโ€™s discuss Tokio Runtime in more detail.

Specifics of Tokio Runtime

Tokio Runtime adds an I/O event loop that starts asynchronous tasks, subscribes to them, and informs local tasks about these events. It interacts with asynchronous system tasks using the mio library, which allows for subscribing to system I/O events and passing notifications. Also, tokio Runtime contains a Scheduler that determines the order for task execution.

Using the tokio::spawn function, we launch a Task โ€” a set of Futures defined as an execution unit โ€” that will be executed by a Processor. A Task is a green thread managed by tokio Runtime.

Hereโ€™s an example of a Task spawned in Tokio:

Rust
use tokio::net::TcpListener; 
 
#[tokio::main] 
async fn main() { 
โ€ฏโ€ฏโ€ฏ let listener = TcpListener::bind("127.0.0.1:8080") 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ .await 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ .expect("failed to bind 8080 socket"); 
 
โ€ฏโ€ฏโ€ฏ while let Ok((connection, _)) = listener.accept().await { 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ tokio::spawn(async move { 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ // doing something with the connection in a separate Task 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ }); 
โ€ฏโ€ฏโ€ฏ } 
}

The Processor that executes the Task represents a system thread. Each Processor has a queue of tasks: it polls a task, executes it, and then moves to the next one.

Processor operation scheme

A Processor with an empty queue can also take a task from a different Processorโ€™s queue โ€” this mechanism is called work-stealing. Thanks to work-stealing, each Processor always has a task to execute.

Work-stealing mechanism in Processor operation

Finally, letโ€™s take a brief look at one more important element you need to know when implementing asynchronous tasks in Rust โ€” state machines.

State machine

When working with asynchronous programming in Rust, the compiler transmits all async functions into a state machine. State machines describe relations between states โ€” behavioral design patterns responsible for changing an objectโ€™s behavior according to changes in its internal state.

Say we have an async of the foo function that connects to some address, reads a message, and sends a response:

Rust
async fn foo() -> io::Result<()> 
{                                                                                         	// State0 
	let mut connection = TcpStream::connect("127.0.0.1:8080").await?;                          	//--------------------------------------------------------------- 
	let mut buf = Vec::new();  // State1 
	let were_read = connection.read(&mut buf).await?; 
	println!("client wrote:{:?}", String::from_utf8_lossy(&buf[0..were_read])); 
    //--------------------------------------------------------------- 
	let message = "hello there"; // State2 
	connection.write(message.as_bytes()).await?; 
    //--------------------------------------------------------------- 
	// State3 
	Ok(()) 
}

For each .await, the compiler creates a separate state where the data for each stage of the foo functionโ€™s operation is stored:

Rust
struct State0Data { 
	addr: &'static str, 
} 
 
struct State1Data { 
	connection: TcpStream, 
	buf: Vec<u8>, 
} 
 
struct State2Data<'a> { 
	message: &'a str, 
	connection: TcpStream, 
} 
 
 
enum FooStateMachine<'a> { 
	State0(State0Data), 
	State1(State1Data), 
	State2(State2Data<'a>), 
}

Then the compiler implements a Future for the FooStateMachine function, where each stage contains code related to that stage as well as a transition to the next state:

Rust
impl<'a> Future for FooStateMachine<'a> { 
	type Output = io::Result<()>; 
 
	fn poll(mut self, cx: &mut Context<'_>) -> Poll<Self::Output> { 
    	match self.as_mut() { 
        	FooStateMachine::State0(State0Data{..}) => { 
            	let mut connection = TcpStream::connect("127.0.0.1:8080").poll(); 
            	match connection { 
                	Poll::Ready(Ok(stream)) => { 
                    	*self = FooStateMachine::State1(State1Data{ 
                        	connection: stream, 
                        	buf: vec![], 
                    	}); 
                	} 
                	Poll::Ready(Err(err)) => return Poll::Ready(Err(err)), 
                	Poll::Pending => return Poll::Pending, 
            	} 
        	}, 
        	FooStateMachine::State1(State1Data{ mut buf, mut connection}) => { 
            	let mut were_read = 0; 
            	match connection.read(&mut buf).poll() { 
                	Poll::Ready(Ok(bytes_read)) => were_read = bytes_read, 
                	Poll::Ready(Err(err)) => return Poll::Ready(Err(err)), 
                	Poll::Pending => return Poll::Pending, 
            	} 
 
            	println!("client wrote:{:?}", String::from_utf8_lossy(&buf[0..were_read])); 
            	*self = FooStateMachine::State2(State2Data{ message: "hello there", connection }); 
        	}, 
        	FooStateMachine::State2(State2Data{message, mut connection }) => { 
            	return match connection.write(message.as_bytes()).poll() { 
                	Poll::Ready(Ok(_)) => Poll::Ready(Ok(())), 
                	Poll::Ready(Err(err)) => Poll::Ready(Err(err)), 
                	Poll::Pending => Poll::Pending, 
            	} 
        	} 
    	} 
 
    	Poll::Pending 
	} 
}

Note that in the example above, we omitted pinning and lifetime to simplify the code.

Also, an asynchronous foo function gets replaced with a synchronous foo function that returns FooStateMachine:

Rust
fn foo() -> FooStateMachine 
{ 
	return FooStateMachine::State0(State0Data{addr: "127.0.0.1:8080" }); 
}

As you can see, the Tokio library provides multiple useful features out of the box, including executors that can immediately handle tasks in multiple threads. Tokio supports work-stealing and channels, allows for writing unit tests, and has strong community support. 

For debugging asynchronous applications written in Rust, you can also go to the following libraries:

  • tracing โ€” application-level tracing for Rust
  • console โ€” diagnostics and debugging tool for asynchronous Rust programs
  • async-backtrace โ€” helps track and monitor the state of asynchronous tasks

There are also many libraries that integrate with Tokio, including:

  • Tonic โ€” Rust gRPC implementation over Tokio
  • Async TCP/TLS/WS/WSS transport support

Letโ€™s move further with our comparison of Rust vs C++ in asynchronous programming and see what alternatives programmers have who work with C++ 20. 

Read also

A Brief Tutorial on Modifying Network Traffic in Windows and Linux

Improve your project with advanced network traffic management! Explore the top network traffic modification techniques to streamline your operations, improve efficiency, and mitigate risks.

Learn more

Coroutines in ะก++ 20

Similarly to Rust, C++ asynchronous programming initially required using complex mechanisms โ€” callbacks and lambda expressions. After the release of C++20, they can now use coroutines โ€” functions that can pause execution and resume it later.

The main difference between the mechanisms for asynchronous programming in Rust and C++ is that in C++, when an async task is launched, a handle of that task is returned. That handle stores the result of the task after some time. Coroutines, on the other hand, launch a green thread and are used in a fire-and-forget style.

There are two types of coroutines:

Coroutine types

1. Stackful coroutines may be suspended within a nested stack frame. When using stackful coroutines, function execution resumes at the same point in the code where it was suspended. During function execution or suspension, local variables are stored on the stack of the corresponding point in code.

2. Stackless coroutines can only be suspended by a top-level function. Any procedure called by this top-level function canโ€™t itself suspend execution. Local variables in such coroutines are located in the stack of the calling code, in a fixed-size buffer that belongs to a particular coroutine.

C++20 introduced different primitives for writing stackless coroutines. A function can be considered a coroutine if it has one of the following keywords (operators):

  • ัo_await
  • co_yield
  • co_return

Now, letโ€™s look closer at the specifics of working with each of these three operators.

Working with the co_await operator

The co_await operator calls a coroutine:

C++
co_await a; 

Using this operator, we can:

  1. Save the local variables in the current function in a heap-allocated object.
  2. Create a callable object (awaiter) responsible for resuming execution of the coroutine.
  3. Call the awaiter created in the previous step.

The compiler creates a coroutine_handle and rewrites the call to a.await_suspend(coroutine_handle) when it detects co_await. This handle is responsible for continuing coroutine execution. 

Working with the co_return operator

This operator indicates that the coroutine should be terminated.

There are three ways we can show that a coroutine needs to be terminated:

  1. Use the co_return e expression, where e is the final value. In this case, the compiler inserts p.return_value(e), where p is the promise object.
  2. Use the co_return expression, which simply ends execution without returning a final value. In this case, the compiler inserts p.return_void().
  3. Allow the coroutine to exit the function, which is equivalent to the second option.

To determine if the coroutine has finished, you can call h.done(), where h is the coroutine handle.

Letโ€™s take a look at the following coroutine example:

C++
#include <concepts> 
#include <coroutine> 
#include <exception> 
#include <iostream> 
 
struct Awaitable 
{ 
public: 
	explicit Awaitable(std::coroutine_handle<>* coroutineHandle): coroutineHandle(coroutineHandle){} 
	~Awaitable() 
	{ 
    	if(this->coroutineHandle) 
        	this->coroutineHandle->destroy(); 
	} 
 
	// required 
	bool await_ready() const noexcept { return false; } 
	void await_suspend(std::coroutine_handle<> h) { *coroutineHandle = h; } 
	void await_resume() const noexcept {} 
private: 
	std::coroutine_handle<>* coroutineHandle; 
}; 
 
struct Task 
{ 
	struct promise_type 
	{ 
    	// required 
    	Task get_return_object() { return {}; } 
    	std::suspend_never initial_suspend() { return {}; } 
    	std::suspend_never final_suspend() noexcept { return {}; } 
    	void unhandled_exception() {} 
 
    	// one return method is required 
    	void return_void(); 
    	//void return_value(T v); 
    	//void yield_value(T v); 
	}; 
}; 
 
Task counter(std::coroutine_handle<>& handle) 
{ 
	Awaitable awaitable(&handle); 
	for (int i = 0;; ++i) 
	{ 
    	co_await awaitable; 
    	std::cout << "counter: " << i << std::endl; 
	} 
} 
 
int main() 
{ 
	std::coroutine_handle<> handle; 
	counter(handle); 
	for (int i = 0; i < 5; ++i) 
	{ 
    	handle(); 
	} 
	return 0; 
}

In this example, we create a Task coroutine that we can manage through a coroutine_handle. The Task must contain the promise object, which affects the behavior of the coroutine at both the beginning and the end of execution.

The promise object should contain the following methods:

  • get_return_object โ€” returns the coroutine object (Task)
  • initial_suspend โ€” returns an awaiter that defines the behavior of the coroutine during the first run (In our case, it returns std::suspend_never, an awaiter from the standard library that forbids suspending coroutine execution during the first run.)
  • final_suspend โ€” returns an awaiter that determines coroutine execution upon completion (We return std::suspend_never, which forbids suspending coroutine execution when exiting.)
  • unhandled_exception โ€” called when an exception occurs and is responsible for handling all sorts of exceptions (In our case, we suppress any exceptions we encounter.)
  • One of three functions that can be called when the value is returned from the coroutine

There are three functions that can be called when the coroutine returns a value:

  • The return_void function returns nothing.
  • The return_value function returns a specific value.
  • The yield_value function suspends the coroutine and returns a value. Note that this function is called when we use the co_yield operator.

If the promise_type misses one of those return methods, weโ€™re faced with undefined behavior when exiting the coroutine.

The counter function represents the body of the coroutine. It creates an awaitable object that controls the behavior of the coroutine on co_await calls. Awaitable should have the following mandatory methods:

  • await_ready โ€” determines whether to make progress on coroutine execution
  • await_suspend โ€” called when coroutine execution is terminated after the await_ready method returns false
  • await_resume โ€” called when the coroutine continues its execution after the await_ready method returns true

After the coroutine has finished its work, we call std::coroutine_handle::destroy to free resources.

The body of the coroutine transforms into the following state machine:

C++
struct CoroFrame 
{ 
	Task::promise_type promise; 
	bool initial_await_resume_called; 
	int state = 0; 
	int i; 
	void operator()(); 
	std::suspend_never initial_suspend_obj; 
	std::suspend_never final_suspend_obj; 
	std::optional<Awaitable> awaitable; 
}; 
 
 
void CoroFrame::operator()() 
{ 
	auto handle = std::coroutine_handle<Task::promise_type>::from_promise(this->promise); 
	try 
	{ 
    	switch (state) 
    	{ 
        	case 0: // initial suspend 
            	if(!this->initial_suspend_obj.await_ready()) 
            	{ 
                	this->initial_suspend_obj.await_suspend(handle); 
                	return; 
            	} 
            	this->initial_suspend_obj.await_resume(); 
 
            	this->initial_await_resume_called = true; 
            	this->state = 1; 
            	return; 
        	case 1: 
            	if(!this->awaitable->await_ready()) 
            	{ 
                	this->awaitable->await_suspend(handle); 
                	return; 
            	} 
 
            	this->awaitable->await_resume(); 
 
            	while(this->i < 5) 
            	{ 
                	std::cout << "counter: " << i << std::endl; 
                	return; 
            	} 
            	this->state = 2; 
            	return; 
        	case 2: 
            	this->promise.return_void(); 
            	goto final_suspend; 
        	default: 
            	// execution should never pass here 
    	} 
    	return; 
	} 
	catch(...) 
	{ 
    	if(!initial_await_resume_called) 
        	throw; 
    	promise.unhandled_exception(); 
	} 
 
final_suspend: 
	if(!this->final_suspend_obj.await_ready()) 
	{ 
    	this->final_suspend_obj.await_suspend(handle); 
    	return; 
	} 
	final_suspend_obj.await_resume(); 
	free(this); 
} 
 
Task counter(std::coroutine_handle<>* handle) { 
 
	CoroFrame* frame = new CoroFrame; 
	Task task{frame->promise.get_return_object()}; 
	// save local variables 
	frame->i = 0; 
 
	// initialize suspend point objects 
	frame->initial_suspend_obj = frame->promise.initial_suspend(); 
	frame->final_suspend_obj = frame->promise.final_suspend(); 
	 
   // save awaiter 
	frame->awaitable.emplace(Awaitable(handle)); 
	(*frame)(); 
	return task; 
}

In this example, we created a so-called Coroutine Frame that stores all local variables, awaitable objects, and the state of the state machine. When we call the counter function, the only two things that happen are the initialization of the CoroFrame and the start of the StateMachine.

The initial_suspend step is executed first, after which the task is returned to the calling code. To make further progress, we need to call the coroutine_handle() function. The compiler knows the offset to the CoroFrame and will call the state machine again, thus starting the coroutine.

Before the resume step, we check if we can make any progress on the task by calling one of the two suspension functions: this->awaitable->await_ready() or this->initial_suspend_obj.await_ready(). After resuming execution of the coroutine, the state transitions to the next execution steps until it encounters an exception or is transferred to final_suspend.

If an exception is thrown, we call the promise.unhandled_exception() function and either return the exception to the calling code (if the exception appears at the initial_suspend stage) or proceed with handling the exception (in all other cases).

Read also

3 Effective DLL Injection Techniques for Setting API Hooks

Detect suspicious and malicious code! Find out how to control and manipulate system behavior and API calls to investigate and protect your software.

Learn more

Working with the co_yield operator

This operator allows for using the co_yield e expression to suspend the execution of a coroutine and return a value from it. This expression is equivalent to co_await p.yield_value(e). The co_yield operator stores the current value in promise_type, from where awaitable can read it.

A typical way of using co_yield is to implement a number generator. Hereโ€™s an example of a generator providing random numbers from one to ten:

C++
#include <coroutine> 
#include <iostream> 
 
struct Generator { 
   struct promise_type { 
  	using handle = std::coroutine_handle<promise_type>; 
 	Generator get_return_object() 
  	{ 
     	return Generator{handle::from_promise(*this) }; 
  	} 
  	std::suspend_always initial_suspend() { return {}; } 
  	std::suspend_always final_suspend() noexcept { return {}; } 
  	std::suspend_always yield_value(int value) { 
     	current_value = value; 
     	return {}; 
  	} 
  	void return_void() {} 
  	void unhandled_exception() {} 
  	int current_value; 
   }; 
   explicit Generator(promise_type::handle handle) : handle(handle) {} 
   ~Generator() 
   { 
  	if (handle) 
     	handle.destroy(); 
   } 
 
   int next() 
   { 
  	handle.resume(); 
  	return handle.promise().current_value; 
   } 
private: 
   promise_type::handle handle; 
}; 
 
Generator myCoroutine() { 
   int x = 0; 
   while (true) { 
  	co_yield x++; 
   } 
} 
 
int main() { 
   auto c = myCoroutine(); 
   int x = 0; 
   while ((x = c.next()) < 10) { 
  	std::cout << x << "\n"; 
   } 
}

In this example, we create a generator that increments a variable. The coroutine resumes execution through the next method, which increments current_value, saves the new value in promise_type, and suspends the coroutine. Then, by reading current_value from promise_type, we can return the original value.

Coroutines in C++20 are flexible, allowing for behavioral control at the start and end of execution as well as for defining suspension and resumption logic. They also offer generators, which are a useful feature on their own. 

However, itโ€™s always important to remember to call the destroy method after the coroutine has finished its work. Also, coroutines should always have special methods to be able to complete their work. For example, if promise_type doesnโ€™t have the return_void method, it will result in undefined behavior when the coroutine finishes.

Now, letโ€™s see how we can use all these event-driven programming tricks in C++20 using a specific library โ€” Boost.Asio.

Related project

Developing a Custom Secrets Management Desktop Application for Secure Password Sharing and Storage

Find out how the Apriorit team engineered a custom secrets management solution that helped our client fortify their data protection, streamline access control, and enhance operational efficiency for their business.

Project details
Developing a Custom Secrets Management Desktop Application for Secure Password Sharing and Storage

Working with Boost.Asio

Since C++ itself doesnโ€™t have built-in networking mechanisms, many C++ developers implement them using Boost.Asio. This library also has a completion token mechanism that developers can use to return results to the calling code. Letโ€™s look closer at this mechanism.

Completion token

A completion token allows us to configure the processing of an operationโ€™s results. Using the completion handler, we can set a callback for processing the result, while the async function returns void. The completion token allows us to return the result to the calling code.

Letโ€™s look at several examples of completion tokens, starting with use_future:

C++
std::future<uint64_t> result = client.async_read_some(boost::asio::buffer(buf), boost::asio::use_future); 
โ€ฏ... 
size_t read_amount = result.get();

The use_future token allows us to return a value in the std::future type, which at some point in time will contain the value weโ€™re waiting for.

Next, we have the yield_context completion token:

C++
void foo(tcp::socket client, boost::asio::yield_context yield) 
{ 
โ€ฏโ€ฏโ€ฏ try  
โ€ฏโ€ฏโ€ฏ { 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ std::vector<uint8_t> buf(1024); 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ size_t read_amout = client.async_read_some(boost::asio::buffer(buf), yield); 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ ... 
โ€ฏโ€ฏโ€ฏ } 
โ€ฏโ€ฏโ€ฏ catch (const std::exception& ex) 
โ€ฏโ€ฏโ€ฏ { 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ ... 
โ€ฏโ€ฏโ€ฏ } 
}

yield_context adds support for stackful coroutines implemented on top of the Boost.Coroutine and Boost.Context libraries. This is a purely library solution for resumable functions that doesnโ€™t require using any keywords.

yield_context represents the object of a current coroutine. By passing yield_context to an async function, we suspend the current function until the operation is completed.

Next, we have the use_awaitable completion token:

C++
boost::asio::awaitable<void> foo(tcp::socket client) 
{ 
โ€ฏโ€ฏโ€ฏ try  
โ€ฏโ€ฏโ€ฏ { 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ std::vector<uint8_t> buf(1024); 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ size_t read_amout = co_await client.async_read_some(boost::asio::buffer(buf), boost::asio::use_awaitable); 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ //... 
โ€ฏโ€ฏโ€ฏ } 
โ€ฏโ€ฏโ€ฏ catch (const std::exception& ex) 
โ€ฏโ€ฏโ€ฏ { 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ //... 
โ€ฏโ€ฏโ€ฏ } 
}

This completion token returns an awaitable type that can be run with the co_await operator in C++20. When we work with the use_awaitable completion token, the async function doesnโ€™t run the asynchronous operation directly. Instead, it only returns awaitable, which in turn runs the operation when the co_await operator is applied to it.

Task management

In Boost.Asio, there are no built-in task scheduling mechanisms. To schedule task execution, we have several options:

  • Create task threads manually
  • Use boost::asio::thread_pool
  • Use boost::fiber in combination with boost::asio to enable scheduling without switching contexts

Hereโ€™s an example of using boost::asio::thread_pool:

C++
boost::asio::awaitable<void> listen(tcp::acceptor& acceptor) 
{ 
โ€ฏโ€ฏโ€ฏ boost::asio::thread_pool pool; 
โ€ฏโ€ฏโ€ฏ while (true) 
โ€ฏโ€ฏโ€ฏ { 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ try 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ { 
โ€ฏ 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ auto client = co_await acceptor.async_accept(boost::asio::use_awaitable); 
โ€ฏ 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ boost::asio::co_spawn(pool.executor(), read_from_client(std::move(client)), boost::asio::detached); 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ } 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ catch (const std::exception& ex) 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ { 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ // handle an exception 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ } 
โ€ฏโ€ฏโ€ฏ } 
} 
โ€ฏ 
int main(int argc, char* argv[]) 
{ 
โ€ฏโ€ฏโ€ฏ try 
โ€ฏโ€ฏโ€ฏ { 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ boost::asio::io_service ctx; 
โ€ฏ 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ tcp::endpoint listen_endpoint(boost::asio::ip::make_address_v4("10.100.4.68"), 8080); 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ  
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ auto acceptor = tcp::acceptor(ctx.get_executor(), listen_endpoint); 
โ€ฏ 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ boost::asio::co_spawn(ctx, listen(acceptor), boost::asio::detached); 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ ctx.run(); 
โ€ฏโ€ฏโ€ฏ } 
โ€ฏโ€ฏโ€ฏ catch (std::exception& e) 
โ€ฏโ€ฏโ€ฏ { 
โ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏโ€ฏ std::cerr << "Exception: " << e.what() << "\n"; 
โ€ฏโ€ฏโ€ฏ } 
}

In this code, we create an io_service that represents the event-loop of asynchronous system events and calls the registered handler for a specific event.

Then we call co_spawn to launch the listen coroutine, where the io_service executor is responsible for resuming this coroutine so that we can accept new clients. co_spawn launches with a detached completion token, thus ignoring any errors or return values from the listen function, and it doesnโ€™t block the main thread.

In the listen function, we create a thread_pool for new clients. Without blocking the execution, we wait for a new client connection and pass the read_from_client coroutine through co_spawn, where the boost thread_pool will be responsible for launching and resuming read_from_client.

Unlike Rust processors, boost::asio::thread_pool doesnโ€™t know how to do work-stealing, although we can pass our own scheduler to it and add similar logic.

Read also

A Comprehensive Guide to Hooking Windows APIs with Python

Enhance the strength of your product with insights from our comprehensive guide! Discover how hooking Windows APIs with Python can revolutionize your business processes and unlock new opportunities for growth. 

Learn more

Cancellation token

In Boost.Asio, objects like sockets and timers support the cancellation of asynchronous operations with the help of member functions such as close or cancel. However, some asynchronous operations also support individual cancellation, which is enabled by setting a cancellation token:

C++
boost::asio::cancellation_signal sig; 
 
boost::asio::co_spawn(pool.get_executor(), read_from_client(std::move(client)), asio::bind_cancellation_slot(sig.slot(), boost::asio::detached)); 
... 
sig.emit():

First, we create a cancellation_signal sig and insert it as a cancellation slot when launching the read_from_client task. Then we can cancel the task using the sig.emit() function.

Error handling and debugging

By default, Boost.Asio throws exceptions to notify user code about an error, but this behavior can be overridden using one of the two completion tokens: redirect_error or as_tuple.

redirect_error allows for passing the error as an out parameter:

C++
size_t read_amount = co_await client.async_read_some(boost::asio::buffer(buf), boost::asio::redirect_error(boost::asio::use_awaitable, ec)); 
if (ec) 
{ 
โ€ฏbreak; 
} 
...

as_tuple works in a similar way, but instead of passing the error via the out parameter, the return value is std::tuple<result_t, boost::system::error_code>.

Boost.Asio allows tracking the execution of asynchronous operations and the connection between their handlers if BOOST_ASIO_ENABLE_HANDLER_TRACKING is defined. When enabled, Boost.Asio will write debug information to the standard error stream.

When comparing C++ Boost.Asio vs Rust Tokio, Boost.Asio is more flexible, offering various options for handling results and managing asynchronous tasks as well as a task cancellation function. At the same time, this library lacks a rich ecosystem around it and is poorly documented. It also doesnโ€™t have channels, which are very convenient for communication between coroutines.

As a result, building a networking application with Boost.Asio will take a lot of time, especially when trying to improve performance, passing a customer thread scheduler or allocator, and so on. When comparing benchmarks, Tokio is slightly faster than Boost.Asio, but this isnโ€™t critical.

Conclusion

Despite the differences between implementing asynchronous programming in Rust and C++20, both languages enable you to build efficient networking applications with asynchronous task execution. Each of these programming languages has a dedicated library for this task โ€” Tokio and Boost.Asio, respectively.

While both of these libraries work great for writing networking applications and both can be used for writing production code, they also have their differences. Tokio comes with a rich selection of useful features, including the work-stealing mechanism. Boost.Asio, on the other hand, allows for flexible configurations which, however, can take extra time to implement due to poor feature documentation.

At Apriorit, we provide both C++ and Rust development services and will gladly assist you with building secure and reactive solutions.

Looking for expert C++ developers? 

Tap into our extensive 20-year knowledge in niche development to bolster your product’s capabilities and gain a competitive edge!

Have a question?

Ask our expert!

Michael-Teslia
Michael Teslia

Program Manager

Tell us about
your project

...And our team will:

  • Process your request within 1-2 business days.
  • Get back to you with an offer based on your project's scope and requirements.
  • Set a call to discuss your future project in detail and finalize the offer.
  • Sign a contract with you to start working on your project.

Do not have any specific task for us in mind but our skills seem interesting? Get a quick Apriorit intro to better understand our team capabilities.