Introducing coroutines

We discussed an example of asynchronous code execution when speaking about GUI applications. GUI components react to user actions by firing corresponding events, which are pushed in the event queue. This queue are then processed one by one by invoking attached handler functions. The described process happens in a loop; that's why we usually refer to the concept as the event loop.

Asynchronous systems are really useful in I/O operations because any input or output operation blocks the execution at the point of I/O call. For example, the following pseudo-code reads a file from a directory and then prints a welcome message to the screen:

auto f = read_file("filename");cout << "Welcome to the app!";process_file_contents(f); ...

Get Expert C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.