Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by bram on 5-5-16.
- //
- #include <stdio.h>
- #include <pthread.h>
- #include "gemiddelde.h"
- #define SIZE 100000
- void *avg(void* args) {
- printf("Starting up thread...");
- float total = 0.0;
- float *array = (float*)args;
- int i;
- for(i = 0; i < SIZE; i++)
- {
- total += array[i];
- }
- float result = total/(float)SIZE;
- float *ptr = &result;
- return (void*)ptr;
- }
- int main()
- {
- /* create threads */
- pthread_t t1, t2, t3, t4;
- if( pthread_create(&t1, NULL, avg, (void*)a1) ||
- pthread_create(&t2, NULL, avg, (void*)a2) ||
- pthread_create(&t3, NULL, avg, (void*)a3) ||
- pthread_create(&t4, NULL, avg, (void*)a4) )
- {
- printf("ERROR CREATING THREADS");
- return 1;
- }
- /* get results */
- void *total1, *total2, *total3, *total4;
- if( pthread_join(t1, total1) ||
- pthread_join(t2, total2) ||
- pthread_join(t3, total3) ||
- pthread_join(t4, total4) )
- {
- printf("ERROR JOINING THREADS");
- return 1;
- }
- /* calculate total avg */
- float total = *((float*)total1) + *((float*)total2) + *((float*)total3) + *((float*)total4);
- total /= 4;
- printf("GEMIDDELDE: %.5f", total);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement