GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Technologies
  • Databases
  • Computer Programming
  • Web Development
  • Java Technologies
  • Computer Science
  • Mobile Development
  • Big Data & Analytics
  • Microsoft Technologies
  • DevOps
  • Latest Technologies
  • Machine Learning
  • Digital Marketing
  • Software Quality

This view

  • Original page
  • New address

GeezerVison is showing www.tutorialspoint.com — C++ Library - <coroutine>

The C++ Standard Library
  • The C Standard Library
  • C++ Library - Home
  • C++ Library - <fstream>
  • C++ Library - <iomanip>
  • C++ Library - <ios>
  • C++ Library - <iosfwd>
  • C++ Library - <iostream>
  • C++ Library - <istream>
  • C++ Library - <ostream>
  • C++ Library - <sstream>
  • C++ Library - <streambuf>
  • C++ Library - <atomic>
  • C++ Library - <complex>
  • C++ Library - <exception>
  • C++ Library - <functional>
  • C++ Library - <limits>
  • C++ Library - <locale>
  • C++ Library - <memory>
  • C++ Library - <new>
  • C++ Library - <numeric>
  • C++ Library - <regex>
  • C++ Library - <stdexcept>
  • C++ Library - <string>
  • C++ Library - <thread>
  • C++ Library - <tuple>
  • C++ Library - <typeinfo>
  • C++ Library - <utility>
  • C++ Library - <valarray>
  • The C++ STL Library
  • C++ Library - <array>
  • C++ Library - <bitset>
  • C++ Library - <deque>
  • C++ Library - <forward_list>
  • C++ Library - <list>
  • C++ Library - <map>
  • C++ Library - <multimap>
  • C++ Library - <queue>
  • C++ Library - <priority_queue>
  • C++ Library - <set>
  • C++ Library - <multiset >
  • C++ Library - <stack>
  • C++ Library - <unordered_map>
  • C++ Library - <unordered_set>
  • C++ Library - <unordered_multiset>
  • C++ Library - <vector>
  • C++ Library - <algorithm>
  • C++ Library - <iterator>
  • The C++ Advanced Library
  • C++ Library - <any>
  • C++ Library - <barrier>
  • C++ Library - <bit>
  • C++ Library - <chrono>
  • C++ Library - <cinttypes>
  • C++ Library - <clocale>
  • C++ Library - <condition_variable>
  • C++ Library - <coroutine>
  • C++ Library - <cstdlib>
  • C++ Library - <cstring>
  • C++ Library - <cuchar>
  • C++ Library - <charconv>
  • C++ Library - <cfenv>
  • C++ Library - <cmath>
  • C++ Library - <ccomplex>
  • C++ Library - <expected>
  • C++ Library - <format>
  • C++ Library - <future>
  • C++ Library - <flat_set>
  • C++ Library - <flat_map>
  • C++ Library - <filesystem>
  • C++ Library - <generator>
  • C++ Library - <initializer_list>
  • C++ Library - <latch>
  • C++ Library - <memory_resource>
  • C++ Library - <mutex>
  • C++ Library - <mdspan>
  • C++ Library - <optional>
  • C++ Library - <print>
  • C++ Library - <ratio>
  • C++ Library - <scoped_allocator>
  • C++ Library - <semaphore>
  • C++ Library - <source_location>
  • C++ Library - <span>
  • C++ Library - <spanstream>
  • C++ Library - <stacktrace>
  • C++ Library - <stop_token>
  • C++ Library - <syncstream>
  • C++ Library - <system_error>
  • C++ Library - <string_view>
  • C++ Library - <stdatomic>
  • C++ Library - <variant>
  • C++ STL Library Cheat Sheet
  • C++ STL - Cheat Sheet
  • C++ Programming Resources
  • C++ Programming Tutorial
  • C++ Useful Resources
  • C++ Discussion
Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Online Resume Builder
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

C++ Library - <coroutine>



Previous
Quiz
Next

The <coroutine> library in C++20, provides the essential building blocks to use coroutines in C++. Coroutines are the modern feature that allows functions to be paused and resumed at certain points. It provides an efficient mechanism for asynchronous programming, generator functions, and cooperative multitasking, making it easier to work with tasks like networking, file I/O.

Unlike traditional functions, coroutines can suspend their execution at certain points and resume later. This is especially useful in scenarios where non-blocking operations(like file I/O or network requests) need to be implemented efficiently.

Including <coroutine> Header

To include the <coroutine> header in your C++ program, you can use the following syntax.

#include <coroutine>

Functions of <coroutine> Header

Below is list of all functions from <coroutine> header.

Sr.No Functions & Description
1 operator=

It assigns the coroutine_handle object.

2 operator coroutine_handle<>

It obtains a type-erased coroutine_handle.

3 done

It checks if the coroutine has completed.

4 operator bool

It checks if the handle represents a coroutine.

5 operator() & resume

It resumes execution of the coroutine.

6 destroy

It destroys a coroutine.

7 promise

It access the promise of a coroutine.

8 address

It exports the underlying address.

Generating a Sequence

In the following example, we are going to create a coroutine that generates a sequence of integers.

#include <iostream>
#include <coroutine>
struct x {
   struct promise_type {
      int a;
      int b = 0;
      x get_return_object() {
         return x {
            this
         };
      }
      std::suspend_always initial_suspend() {
         return {};
      }
      std::suspend_always final_suspend() noexcept {
         return {};
      }
      void return_void() {}
      void unhandled_exception() {}
      std::suspend_always yield_value(int val) {
         a = val;
         return {};
      }
      int getNext() {
         return b++;
      }
   };
   promise_type * promise;
   x(promise_type * p): promise(p) {}
   int next() {
      promise -> yield_value(promise -> getNext());
      return promise -> a;
   }
};
x generateSequence() {
   for (int i = 0; i < 4; ++i) {
      co_yield i;
   }
}
int main() {
   auto sequence = generateSequence();
   for (int i = 0; i < 4; ++i) {
      std::cout << "Next value: " << sequence.next() << '\n';
   }
   return 0;
}

Output

Following is the output of the above code −

Next value: 0
Next value: 1
Next value: 2
Next value: 3
Tutorix - AI Tutor
Previous Quiz Next
Advertisements
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.