Boost logo

Geometry :

Subject: Re: [geometry] volatile polygon
From: Adam Wulkiewicz (adam.wulkiewicz_at_[hidden])
Date: 2013-11-25 07:13:53


Hi,

Long Guo wrote:
> Dear sir,
>
> I have two threads using the same polygon.
> One thread is modifying the polygon.
> And I want the other thread to use the modified polygon,
> so I need to define the polygon as volatile.
> However, when I use *(volatile polygon*)&mypolygon,
> I get some error as follows:
> error: no type named ‘type’ in ‘struct
> boost::geometry::dimension<volatile
> boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double>
> > >’.
>
> What's wrong with this usage?
>

boost::geometry::dimension (probably as well as some other functions)
isn't volatile-aware. Barend maybe we should use remove_volatile<> as well?.

Still, using 'volatile' isn't a proper way of synchronizing data between
threads. For better ways see:
http://www.boost.org/doc/libs/1_55_0/doc/html/thread/synchronization.html
You should for example use Mutex to synchronize access. This way you
could secure just some operations like operator[] access, copying of the
polygon or moving (in the case of exclusive ownership).

Some general solution would look like this:

class producer_thread
{
public:
// your thread body
void operator()()
{
// some code...

// modification of the local copy of polygon
polygon local_poly;
// ...

// copying/moving of local to shared
{
boost::lock_guard<boost::mutex> lock(m_mut);
m_poly = boost::move(local_poly);
}
}

polygon get_poly()
{
boost::lock_guard<boost::mutex> lock(m_mut);
return m_poly;
//or
//return boost::move(m_poly);
}

private:
polygon m_poly;
boost::mutex m_mut;
};

Then you can run the thread:

producer_thread producer;
boost::thread t((boost::ref(producer)));

and copy/move polygon to the current thread:

polygon p = producer.get_poly();

Regards,
Adam


Geometry list run by mateusz at loskot.net