问题
I am trying to create a multithreaded spatial index using rtree from Boost.Geometry, however I am unable to determine if this is thread safe. I do not see any locking mechanisms inside rtree.hpp, but my C++/Boost knowledge is at a beginners level.
Is Boost.Geometry.index.rtree thread safe in any way? If not, what would be the optimal approach to use it with multiple threads in a safe manner (e.g. mutex lock between insert() calls? Am I able to query() at the same time as insert()?). Specifically I'm trying to get better query (read) performance.
回答1:
Is Boost.Geometry.index.rtree thread safe in any way?
No
If not, what would be the optimal approach to use it with multiple threads in a safe manner (e.g. mutex lock between insert() calls?
Optimal? Depends.
You need mutual exclusion. You can do this with spinlocks, simple mutex, shared/upgradable mutex etc.
Am I able to query() at the same time as insert()?).
Certainly not. That's called a data race and it's what you need the mutual exclusion (aka monitor) for in the first place.
Specifically I'm trying to get better query (read) performance.
Adding threads doesn't make things faster. It makes things slower. Always.
The trick is that you can do other things at the same time.
You /can/ run multiple read-only operations in parallel. Usually, library containers are safe to use from multiple threads for read-only operations (although you might want to do a quick scan for any mutable
members hidden( in the implementation).
来源:https://stackoverflow.com/questions/28266656/can-i-use-boost-geometry-index-rtree-with-threads