Advertisement
nex036ara

lock_guard

Apr 5th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <thread>
  4. #include <cmath>
  5. #include <mutex>
  6. using namespace std;
  7.  
  8. mutex m;
  9.  
  10. struct Point {
  11.     double x, y;
  12.     };
  13.  
  14. typedef vector<Point>::iterator ci;
  15.  
  16. class Circle {
  17.  
  18.     private:
  19.         Point a;
  20.         double r;
  21.  
  22.     public:
  23.         Circle(const Point& t, double r_): a(t), r(r_) {}
  24.  
  25.         double distance(Point &t){
  26.            double b = sqrt( pow(t.x- a.x,2) + pow(t.y- a.y,2) )- r;
  27.  
  28.            return abs(b);
  29.                     }
  30.  
  31.     void operator()(ci begin, ci end, Point &min){
  32.  
  33.         lock_guard<mutex> lock(m);
  34.  
  35.            Point minimal = *begin;
  36.             for(; begin!=end; ++begin){
  37.                 if( distance(*begin) < distance(minimal)) {
  38.                     minimal=*begin;
  39.                     }
  40.                 } if (distance(minimal)< distance(min))
  41.                     min = minimal;
  42.  
  43.         }
  44.  
  45. };
  46.  
  47. int main()
  48. {
  49.     int n=3;
  50.     Circle c({0,0},3);
  51.     vector<Point> points = {{0,0},{1,1},{2,2},{3,3},{0,3},{6,3},{-63,3},{5,5},{6,6}};
  52.     Point min = points[0];
  53.  
  54.     thread t1 = thread(c, points.begin(), points.begin()+3, ref(min));
  55.     thread t2 = thread(c, points.begin()+3, points.begin()+6, ref(min));
  56.     thread t3 = thread(c, points.begin()+6,points.end(), ref(min));
  57.  
  58.     t1.join();
  59.     t2.join();
  60.     t3.join();
  61.  
  62.     cout<<"Tacka ("<<min.x<<","<<min.y<<") je najbliza kruznici." <<endl;
  63.  
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement