MistyD
MistyD

Reputation: 17223

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

Upvotes: 3

Views: 152

Answers (1)

Andy Prowl
Andy Prowl

Reputation: 126412

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. [...]

Upvotes: 8

Related Questions