Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing thread safe containers [closed]

Could someone recommend good book on how to write thread safe containers?
thanks

like image 734
smallB Avatar asked Apr 23 '26 13:04

smallB


2 Answers

Thread safe containers are no silver bullet!

E.g. this code is not thread safe, whatever container you use:

if (!container.has(value)) container.add(value);

Neither is:

container[value] = container[value] + 42;

Code can be made thread safe, but it requires a lot more than thread safe containers.

It's even worse: thread safe containers won't buy you much. They may help for simple cases you'll find in text books, but in the more complicated cases that exist in practice, you will need a lock anyway (see above for typical cases). Using a thread safe container in that case will be purely overhead.

Have a look at Java, as it started with thread safe containers. After a couple of years Java switched to thread unsafe containers and deprecated the old, thread safe, containers.

So my advice: do not look for thread safe containers. Instead, look for a good book how to write thread safe code.

UPDATE for clarification: I'm talking about the standard containers, like vector, map, and string.

like image 132
Sjoerd Avatar answered Apr 26 '26 03:04

Sjoerd


1024cores.net is chock full of good articles about writing thread safe code.

like image 40
Jeff Foster Avatar answered Apr 26 '26 04:04

Jeff Foster