Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SUITE(ConcurrentHashMapTest)
- {
- struct Foo
- {
- Foo(int i) : dummy(i) {}
- int dummy;
- };
- typedef boost::shared_ptr<Foo> FooPtr;
- template<class T>
- struct shared_ptr_hash_compare
- {
- static size_t hash(const boost::shared_ptr<T>& ptr)
- {
- return (size_t)ptr.get();
- }
- static bool equal(const boost::shared_ptr<T>& a, const boost::shared_ptr<T>& b)
- {
- return a == b;
- }
- };
- typedef tbb::concurrent_hash_map<FooPtr, FooPtr, shared_ptr_hash_compare<Foo> > Map;
- struct Accessor
- {
- Accessor(Map& m) : map(m) {}
- void run()
- {
- while(map.size() > 0)
- {
- BOOST_FOREACH(Map::value_type& el, map)
- {
- boost::this_thread::yield();
- el.second->dummy += 1;
- }
- }
- }
- Map& map;
- };
- struct Deleter
- {
- Deleter(Map& m) : map(m) {}
- void run()
- {
- while(map.size() > 0)
- {
- Map::iterator it = map.begin();
- if(it != map.end())
- map.erase(it->first);
- boost::this_thread::yield();
- }
- }
- Map& map;
- };
- TEST(ConcurrentAccessToSharedPtrs)
- {
- Map map;
- for(size_t i=0; i<10000; ++i)
- {
- FooPtr foo(new Foo(i));
- map.insert(std::make_pair(foo, foo));
- }
- Accessor accessor(map);
- Deleter deleter(map);
- boost::thread_group thg;
- thg.create_thread(boost::bind(&Accessor::run, boost::ref(accessor)));
- thg.create_thread(boost::bind(&Deleter::run, boost::ref(deleter)));
- thg.join_all();
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement