Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you need locking with only reading

I wanted to know that is it safe to assume that if multiple threads are accessing a single static container (boost::unordered_map) there is no need for locking the access to the container if multiple threads are only reading data from it. and no writing is done

like image 826
MistyD Avatar asked Dec 01 '25 00:12

MistyD


1 Answers

When multiple threads are only reading and performing no write operation, you do not need to synchronize access.

Paragraph 1.10 of the C++11 Standard defines conflicting operations with respect to data races as:

Two expression evaluations conflict if one of them modifies a memory location (1.7) and the other one accesses or modifies the same memory location.

And then of course, per 1.10/21:

The execution of a program contains a data race if it contains two conflicting actions in different threads, at least one of which is not atomic, and neither happens before the other. [...]

like image 128
Andy Prowl Avatar answered Dec 02 '25 15:12

Andy Prowl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!