Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4.  
  5. #define THREAD_COUNT 8
  6. #define WORK 25000
  7.  
  8. void* stress(void* x)
  9. {
  10.     int j;
  11.     for(j = 0; j < WORK; ++j){
  12.         int array[100][100];
  13.         int i, k;
  14.         for (i = 0; i < 100; ++i) {
  15.             for (k = 0 ; k < 100; ++k) { array[i][k] += i + k * rand();}
  16.         }
  17.     }
  18.     return NULL;
  19. }
  20.  
  21. int main()
  22. {
  23.     pthread_t   thread_ids[THREAD_COUNT];
  24.     int         thread_args[THREAD_COUNT];
  25.     int         i;
  26.  
  27.     // Spawn the threads
  28.     for (i = 0; i < THREAD_COUNT; ++i)
  29.     {
  30.         thread_args[i] = i;
  31.         pthread_create(
  32.             &thread_ids[i]              // Thread ID
  33.             ,NULL                       // Thread Attributes (NULL is fine)
  34.             ,stress                      // Function thread will call
  35.             ,(void*) &thread_args[i]    // Arguments passed to function
  36.         );
  37.     }
  38.  
  39.     // Wait for threads to finish
  40.     for (i = 0; i < THREAD_COUNT; ++i)
  41.     {
  42.         pthread_join(thread_ids[i], NULL);
  43.     }
  44.  
  45.     printf("done");
  46.     return 1;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement