Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include <iostream>
  5. #include <ctime>
  6.  
  7. using namespace std;
  8.  
  9. #define beg 10.0
  10. #define end 20.0
  11. #define NTh 4//количество потоков
  12. #define NRec 1000.0//количество прямоугольников
  13.  
  14. double Sum = 0;
  15. double x=beg+((end - beg) / (2*NRec));
  16.  
  17. void WINAPI ThreadProc()
  18. {
  19. for (double i = 0; i < NRec / NTh; i++)
  20. {
  21. Sum += (x * x -x -5 )*((end - beg) / NRec);
  22. x += (end - beg) / NRec;
  23. }
  24.  
  25. }
  26.  
  27. int main(int argc, char* argv[])
  28. {
  29. setlocale(LC_CTYPE, "rus");
  30. HANDLE Thread[NTh];
  31. double start_time = clock();
  32. for (int i = 0; i < NTh; i++)
  33. Thread[i] = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)ThreadProc, 0, 0, 0);
  34. WaitForMultipleObjects(NTh, Thread, TRUE, INFINITE);
  35. double finish_time = clock();
  36. cout << "number rectangles: " << NRec << endl;
  37. cout << "number threads: " << NTh<< endl;
  38. cout << "time: " << (finish_time - start_time) / 1000 << endl;
  39. cout << "result : " << Sum << endl;
  40. cout << "error: " << Sum/(6400/3.) << endl;
  41. system("pause");
  42. return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement