Advertisement
Guest User

Problems with threads

a guest
Oct 22nd, 2012
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <conio.h>
  5. #include <windows.h>
  6. #include <process.h>
  7. #include <math.h>
  8.  
  9. int n = 100000;
  10. int num_testes = 5;
  11. long vetor[100000];
  12. long resp_threadless[100000];
  13. long resp_thread[100000];
  14. long pos = 0;
  15.  
  16. //HANDLE  hCountMutex = CreateSemaphore(NULL, 1, 1, NULL);
  17.  
  18. void buildvector() {
  19.   srand ( time(NULL) );
  20.   for (int i = 0; i < n; ++i) {
  21.     vetor[i] = (rand() % n) + 1;
  22.   }
  23. }
  24.  
  25. DWORD WINAPI function_th( LPVOID lpParam ) {
  26.   for (long j = 0; j < n; ++j) {
  27.     int count = 0;
  28.     double x = vetor[j];
  29.     while (x > 1.0) {
  30.       x = sqrt(x);
  31.       ++count;
  32.     }
  33.     resp_threadless[j] = count;
  34.   }
  35. }
  36.  
  37. void function() {
  38.   for (long j = 0; j < n; ++j) {
  39.     int count = 0;
  40.     double x = vetor[j];
  41.     while (x > 1.0) {
  42.       x = sqrt(x);
  43.       ++count;
  44.     }
  45.     resp_threadless[j] = count;
  46.   }
  47.  
  48. }
  49.  
  50. void threaded(int num_threads) {
  51.   double time_start, time_stop, total_time, sum_times;
  52.   sum_times = 0;
  53.   printf("Threaded %d\n", num_threads);
  54.   for (int i = 0; i < num_testes; ++i) {
  55.     time_start = (double)clock()/CLOCKS_PER_SEC;
  56.    
  57.     pos = 0;
  58.     HANDLE hThreadArray[num_threads];
  59.     DWORD dwThreads[num_threads];
  60.     for (int j = 0; j < num_threads; ++j) {
  61.       hThreadArray[j] = CreateThread(NULL, 0, function_th, (void*)j, 0, &(dwThreads[j]));
  62.     }
  63.     WaitForMultipleObjects(num_threads, hThreadArray, TRUE, INFINITE);
  64.     for(int j = 0; j < num_threads; ++j)
  65.     {
  66.       CloseHandle(hThreadArray[j]);
  67.     }
  68.    
  69.     time_stop = (double)clock()/CLOCKS_PER_SEC;
  70.     total_time = (time_stop - time_start)*1000;
  71.     printf("%d/%d = %f\n", i+1, num_testes, total_time);
  72.     sum_times += total_time;
  73.   }
  74.   printf("Media: %f\n\n", sum_times/num_testes);
  75. }
  76.  
  77. void threadless() {
  78.   double time_start, time_stop, total_time, sum_times;
  79.   sum_times = 0;
  80.   printf("Threadless\n");
  81.   for (int i = 0; i < num_testes; ++i) {
  82.     time_start = (double)clock()/CLOCKS_PER_SEC;
  83.    
  84.     function();
  85.    
  86.     time_stop = (double)clock()/CLOCKS_PER_SEC;
  87.     total_time = (time_stop - time_start)*1000;
  88.     printf("%d/%d = %f\n", i+1, num_testes, total_time);
  89.     sum_times += total_time;
  90.   }
  91.   printf("Media: %f\n\n", sum_times/num_testes);
  92. }
  93.  
  94. int main() {
  95.   buildvector();
  96.   threadless();
  97.   threaded(1);  
  98.   threaded(2);
  99.   threaded(4);
  100.   threaded(8);
  101.   threaded(16);
  102.   getch();
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement