Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <vector>
- #include <time.h>
- #include <cmath>
- using namespace std;
- typedef vector<double>::const_iterator ci;
- void f(ci begin, ci end, double &zbir)
- {
- zbir = 0;
- for (ci it = begin; it != end; ++it)
- zbir += *it;
- cout << "Zbir je: " << zbir << endl;
- }
- int main()
- {
- double zbir1 = 0, zbir2 = 0;
- int s = 0; // vector size
- thread t1, t2;
- vector<double> v;
- srand(time(NULL));
- for (int i=0; i<10000; i++)
- v.push_back(rand() % 100+1);
- s = v.size() / 2;
- t1 = thread(f,v.begin(), v.begin()+s, ref(zbir1));
- t2 = thread(f,v.begin()+s, v.end(), ref(zbir2));
- t1.join();
- t2.join();
- cout << "Ukupan zbir je: " << zbir1+zbir2 << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement