Advertisement
Lauda

Untitled

Feb 27th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <vector>
  4. #include <time.h>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. typedef vector<double>::const_iterator ci;
  10. void f(ci begin, ci end, double &zbir)
  11. {
  12.     zbir = 0;
  13.     for (ci it = begin; it != end; ++it)
  14.         zbir += *it;
  15.  
  16.     cout << "Zbir je: " << zbir << endl;
  17.  
  18. }
  19.  
  20. int main()
  21. {
  22.     double zbir1 = 0, zbir2 = 0;
  23.     int s = 0; // vector size
  24.     thread t1, t2;
  25.     vector<double> v;
  26.  
  27.     srand(time(NULL));
  28.     for (int i=0; i<10000; i++)
  29.         v.push_back(rand() % 100+1);
  30.  
  31.     s = v.size() / 2;
  32.     t1 = thread(f,v.begin(), v.begin()+s, ref(zbir1));
  33.     t2 = thread(f,v.begin()+s, v.end(), ref(zbir2));
  34.     t1.join();
  35.     t2.join();
  36.  
  37.     cout << "Ukupan zbir je: " << zbir1+zbir2 << endl;
  38.  
  39.  
  40.  
  41.     return 0;
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement