xref: /netbsd-src/external/mit/libuv/dist/docs/code/thread-create/main.c (revision 5f2f42719cd62ff11fd913b40b7ce19f07c4fd25)
10e552da7Schristos #include <stdio.h>
20e552da7Schristos 
30e552da7Schristos #include <uv.h>
40e552da7Schristos 
hare(void * arg)50e552da7Schristos void hare(void *arg) {
60e552da7Schristos     int tracklen = *((int *) arg);
70e552da7Schristos     while (tracklen) {
80e552da7Schristos         tracklen--;
9*5f2f4271Schristos         uv_sleep(1000);
100e552da7Schristos         fprintf(stderr, "Hare ran another step\n");
110e552da7Schristos     }
120e552da7Schristos     fprintf(stderr, "Hare done running!\n");
130e552da7Schristos }
140e552da7Schristos 
tortoise(void * arg)150e552da7Schristos void tortoise(void *arg) {
160e552da7Schristos     int tracklen = *((int *) arg);
170e552da7Schristos     while (tracklen) {
180e552da7Schristos         tracklen--;
190e552da7Schristos         fprintf(stderr, "Tortoise ran another step\n");
20*5f2f4271Schristos         uv_sleep(3000);
210e552da7Schristos     }
220e552da7Schristos     fprintf(stderr, "Tortoise done running!\n");
230e552da7Schristos }
240e552da7Schristos 
main()250e552da7Schristos int main() {
260e552da7Schristos     int tracklen = 10;
270e552da7Schristos     uv_thread_t hare_id;
280e552da7Schristos     uv_thread_t tortoise_id;
290e552da7Schristos     uv_thread_create(&hare_id, hare, &tracklen);
300e552da7Schristos     uv_thread_create(&tortoise_id, tortoise, &tracklen);
310e552da7Schristos 
320e552da7Schristos     uv_thread_join(&hare_id);
330e552da7Schristos     uv_thread_join(&tortoise_id);
340e552da7Schristos     return 0;
350e552da7Schristos }
36