khizmax / libcds

A C++ library of Concurrent Data Structures
http://libcds.sourceforge.net/doc/cds-api/index.html
Boost Software License 1.0
2.57k stars 358 forks source link

Unclear statements about iterators #62

Closed Kri-Ol closed 7 years ago

Kri-Ol commented 8 years ago

It is probably not a code issue, but I don't understand if container(s) (say, skip list) have iterators which are used to update value(s) from different threads.

khizmax commented 8 years ago

Iterators in concurrent datastructs (DS) are tricky. Today all DS in libcds except FeldmanHashSet/Map have iterator only for debug purposes. In any thread you can safely remove an element the iterator i points to but if another thread removes the next element then ++i can crash. So, the iterators are safe if no removal operation taken place. Next point, all safe reclamation schema (Hazard Pointer, RCU) from libcds protects DS but not fields of element. Libcds guarantees that while your iterator points to an element it cannot be deleted. Not more. If you want to update any fields of element you should provide your own protecting stuff at the element level - for example, a mutex or a spinlock incorporated into element, or use atomic operations for updating if possible, or anything else.