c++ - Can I make a thread-safe std::atomic<vector<int>>? -
i'm having function needs executed n=1000
times. functions monte carlo style simulation , returns int
result. i'd run nthreads=4
in parallel. whenever thread finishes 1 cycle, should put result in std::vector<int>
. thus, after 1000 cycles, i've vector of 1000 int
s can examined statistics.
since std::vector
not thread-safe, thought std::mutex
(which surely work).
but wonder if can declare vector atomic , around mutexes? possible have std::atomic<std::vector<int>>
? , can use push_back
etc. on it?
you don't need to. totally okay, access ja std::vector
multiple threads, if
- you read objects
- you write different objects
so make sure, create vector of size n=1000
, depending on thread number (1 4) assign elements 0-249, 250-499 etc. threads.
so each of thread computes n/nthreads
elements.
Comments
Post a Comment