From cbeac6412840ea0e978f14da08c5a5e02796c7e2 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Tue, 31 Mar 2015 14:55:32 +0200 Subject: Pthreads testing. --- pthreads.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 pthreads.c diff --git a/pthreads.c b/pthreads.c new file mode 100644 index 0000000..87945fd --- /dev/null +++ b/pthreads.c @@ -0,0 +1,45 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +#define NUM_THREADS 100 + +typedef void *(* thread_func)(void *); + +void *somefunction(int *arg) +{ + int loops = rand(); + + for (int i = 0; i < loops; i++) { + pthread_yield(); + for (;i < loops; i++); + } + + printf("%d:\t%d\n", *arg, loops); + + return NULL; +} + +int main(void) +{ + srand(time(NULL)); + + pthread_t threads[NUM_THREADS]; + int args[NUM_THREADS]; + + for (int i = 0; i < NUM_THREADS; i++) { + args[i] = i; + if (pthread_create(&threads[i], NULL, (thread_func)somefunction, args + i)) + return EXIT_FAILURE; + } + + for (int i = 0; i < NUM_THREADS; i++) + if (pthread_join(threads[i], NULL)) + return EXIT_FAILURE; + + return EXIT_SUCCESS; +} -- cgit v1.2.3-54-g00ecf