diff options
author | Prefetch | 2023-07-16 15:04:44 +0200 |
---|---|---|
committer | Prefetch | 2023-07-16 15:04:44 +0200 |
commit | 1a2e93c752a6b835423cd8bee609b8ec6c3f1262 (patch) | |
tree | a1c1edccbb110edc9b74b2e7ecbf47196a14c418 /tests/test01.c |
Initial commit for publication
Diffstat (limited to 'tests/test01.c')
-rw-r--r-- | tests/test01.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/test01.c b/tests/test01.c new file mode 100644 index 0000000..198dac7 --- /dev/null +++ b/tests/test01.c @@ -0,0 +1,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); +} |