diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2015-03-31 14:55:32 +0200 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2015-03-31 14:55:32 +0200 |
commit | cbeac6412840ea0e978f14da08c5a5e02796c7e2 (patch) | |
tree | dd64121c43ca44872fab10f03cff10f934f45a0d | |
parent | 88344ddbd467e20b3f2a3ae833a6370277aa5c82 (diff) | |
download | c-stuff-cbeac6412840ea0e978f14da08c5a5e02796c7e2.tar.gz c-stuff-cbeac6412840ea0e978f14da08c5a5e02796c7e2.tar.xz c-stuff-cbeac6412840ea0e978f14da08c5a5e02796c7e2.zip |
Pthreads testing.
-rw-r--r-- | pthreads.c | 45 |
1 files changed, 45 insertions, 0 deletions
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 <assert.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <unistd.h> + +#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; +} |