Wondered if I could get your thoughts on what I should do in this scenario.
Assume I have between 4 to 8 threads and I have a vector of values which will never be written to, only read by the threads.
I have the option of creating a copy of the vector for each thread and then no thread locking between the threads, trying to access a shared copy. Or, I could lock one copy of the vector and make all threads access that.
What is the latency of a thread lock, in comparison to copying the vector? How big would the vector have to be, to make the overhead of the lock faster than copying the vector?
YES for the scenario you mention, it is perfectly Thread Safe.
If the data is read-only for the lifetime of all the threads that read it, then yes, it's perfectly fine to read without synchronization.
Multiple threads can also read data from the same FITS file simultaneously, as long as the file was opened independently by each thread. This relies on the operating system to correctly deal with reading the same file by multiple processes.
No. Managing the vector class across threads is not safe, you need to use some synchronization mechanism (e.g. a mutex) to protect read/write access to the std::vector<> instance.
If no thread ever writes to it, you can safely share it without any lock or copy. Data races can only occur if there are write accesses involved.
Assuming the vector doesn't change after the threads start accessing it, there is no need to lock it. If the thread that populates the vector finishes populating before the reader threads start reading, you are safe without any further synchronization.
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