summaryrefslogtreecommitdiff
path: root/tests/test01.c
blob: 198dac75cb35745c43e7bf784bb1f15c24b4e8f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "linen.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>


#define NUM_THREADS 10


/* Argument for thread function */
typedef struct {
	useconds_t delay;
	int id;
} f_arg_t;


/* Function for threads to run */
void* f(void* arg) {
	f_arg_t* a = (f_arg_t*)arg;
	usleep(a->delay);
	printf("    Hello from thread #%d!\n", a->id);
	return (void*)&(a->delay);
}


void main() {
	printf("\x1B[1mTEST 01: threads print after random delay:\x1B[0m\n");

	/* Thread handles and arguments */
	linen_thread_t ts[NUM_THREADS];
	f_arg_t args[NUM_THREADS];

	/* Set arguments */
	srand(time(NULL));
	for (int i = 0; i < NUM_THREADS; i++) {
		args[i].delay = (useconds_t)rand() % 1000000;
		args[i].id = i;
	}

	/* Spawn threads */
	for (int i = 0; i < NUM_THREADS; i++) {
		int r = linen_thread_create(&ts[i], f, (void*)&args[i]);
		if (r) {
			printf("    Failed to spawn thread #%d with error %d\n", i, r);
		}
	}

	/* Wait for threads to finish */
	usleep(1200000);
}