forked from cory/tildefriends
Merge branches/quickjs to trunk. This is the way.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3621 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
44
deps/libuv/docs/code/queue-work/main.c
vendored
Normal file
44
deps/libuv/docs/code/queue-work/main.c
vendored
Normal file
@ -0,0 +1,44 @@
|
||||
#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);
|
||||
}
|
Reference in New Issue
Block a user