Created
March 3, 2014 15:46
-
-
Save sukinull/9327715 to your computer and use it in GitHub Desktop.
This file contains 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 <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <uv.h> | |
#define FIB_UNTIL 25 | |
uv_loop_t *loop; | |
long fib_(long t) { | |
if (t == 0 || t == 1) | |
return 1; | |
else | |
return fib_(t-1) + fib_(t-2); | |
} | |
void fib(uv_work_t *req) { | |
int n = *(int *) req->data; | |
if (random() % 2) | |
sleep(1); | |
else | |
sleep(3); | |
long fib = fib_(n); | |
fprintf(stderr, "%dth fibonacci is %lu\n", n, fib); | |
} | |
void after_fib(uv_work_t *req, int status) { | |
fprintf(stderr, "Done calculating %dth fibonacci\n", *(int *) req->data); | |
} | |
int main() { | |
loop = uv_default_loop(); | |
int data[FIB_UNTIL]; | |
uv_work_t req[FIB_UNTIL]; | |
int i; | |
for (i = 0; i < FIB_UNTIL; i++) { | |
data[i] = i; | |
req[i].data = (void *) &data[i]; | |
uv_queue_work(loop, &req[i], fib, after_fib); | |
} | |
return uv_run(loop, UV_RUN_DEFAULT); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment