Thread-safe data structures

Posted by Inso Reiges on Stack Overflow See other posts from Stack Overflow or by Inso Reiges
Published on 2010-03-19T10:37:29Z Indexed on 2010/03/19 10:41 UTC
Read the original article Hit count: 468


I have to design a data structure that is to be used in a multi-threaded environment. The basic API is simple: insert element, remove element, retrieve element, check that element exists. The structure's implementation uses implicit locking to guarantee the atomicity of a single API call. After i implemented this it became apparent, that what i really need is atomicity across several API calls. For example if a caller needs to check the existence of an element before trying to insert it he can't do that atomically even if each single API call is atomic:

if(!data_structure.exists(element)) {

The example is somewhat awkward, but the basic point is that we can't trust the result of exists call anymore after we return from atomic context (the generated assembly clearly shows a minor chance of context switch between the two calls).

What i currently have in mind to solve this is exposing the lock through the data structure's public API. This way clients will have to explicitly lock things, but at least they won't have to create their own locks. Is there a better commonly-known solution to these kinds of problems? And as long as we're at it, can you advise some good literature on thread-safe design?

Thank you.

© Stack Overflow or respective owner

Related posts about synchronization

Related posts about multithreading