If I've understood correctly, since C++11 it has been safe to call const member functions of a container concurrently and modify the elements of a container as long as the container itself is not modified as part of the operation (as seen from e.g. the table concerning thread safety in cppreference.com). Since std::valarray is not listed in the containers section of the (draft) standard, I'm unsure if thread safety also applies to it. In other words,
I would like to use std::valarray for a multidimensional array of numbers that would be filled using multiple threads.
No, they're not thread-safe. The standard containers are simply not thread-safe, period. There is however a limited amount of thread safety: If every thread accesses a different element, and no element is accessed by two distinct threads at any given time, then that's fine.
The SGI implementation of STL is thread-safe only in the sense that simultaneous accesses to distinct containers are safe, and simultaneous read accesses to to shared containers are safe.
If I am reading your question correctly, [res.on.data.races]
protects distinct slices from participating in data races, under
A C++ standard library function shall not directly or indirectly access objects accessible by threads other than the current thread unless the objects are accessed directly or indirectly via the function's arguments, including
this
.
[container.requirements.dataraces]
adds extra protection around modifications to distinct elements, which strictly valarray
lacks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With