Is reading from a dict always threadsafe?

Writing to a dict is not thread safe, but reading appears to be:

test_dict = Dict(i => i+1 for i in 1:10_000)
ThreadsX.map(x -> getindex(test_dict, x), 1:10_000)

> 10000-element Vector{Int64}:
     2
     3
     4
     .
     .
     .

Is this guaranteed – e.g., can I safely write code that has multiple threads read from a dict, or should I use a mechanism like channels if I need that to work?

In general, reads to Dicts (and almost any data structure will be thread safe) if there aren’t any writes.

3 Likes