Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

Intro to C++ Coroutines: Concept

void f() {
  int a = 0;
  ++a;
  ...suspend/yield magic()...
  print(a);
}
void Function(ExecutionState* state) {
  switch(*state){
  case 0:...
  case 1:...
  }
}
void Function(ExecutionState* state) {
  int a = 0;
  switch(*state){
  case 0: a++; *state = 1; return;
  case 1: printf("%d", a); return;
  }
}
struct ExecutionState{ int state; int a;};

void Function(ExecutionState* state) { //assume that state is initialized with zeroes
  switch(*state){
  case 0: state->a++; state->state = 1; return;
  case 1: printf("%d", state->a); return;
  }
}

About KDAB

Tags

blogc++

1 Comment

18 - Nov - 2023

Abel Sen