Created
May 8, 2019 14:16
-
-
Save benman1/ad5149c694bbb249e1bb23b9b008fc49 to your computer and use it in GitHub Desktop.
concurrent for loop with fibers in c++ using the fiberize library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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