Skip to content

Instantly share code, notes, and snippets.

@benman1
Created May 8, 2019 14:16
Show Gist options
  • Save benman1/ad5149c694bbb249e1bb23b9b008fc49 to your computer and use it in GitHub Desktop.
Save benman1/ad5149c694bbb249e1bb23b9b008fc49 to your computer and use it in GitHub Desktop.
concurrent for loop with fibers in c++ using the fiberize library
#include <fiberize/fiberize.hpp>
#include <iostream>
// after https://github.com/fiberize/fiberize/blob/master/fiberize/examples/helloworld/main.cpp
using namespace fiberize;
#define VECTOR_SIZE 100
int vector[VECTOR_SIZE]
int main() {
FiberSystem system;
FiberRef mainThread = system.fiberize();
auto square_fun = system.fiber([mainThread] (int n) {
vector[n] = n * n;
if (n == VECTOR_SIZE) {
mainThread.send(finished);
}
});
for (size_t i=0; i<VECTOR_SIZE; i++) {
square_fun.copy().run_(i);
}
finished.await();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment