问题
I have an application that I'm trying to make multithreaded. Each thread will access a large chunk of read-only data.
Is is okay if multiple threads access the data simultaneously? I know that if the data were not read-only, I would need to use mutexes or some other form of synchronization to prevent race-conditions. But I'm wondering if it's okay to read the data without regard to synchronization.
The data in question will not be modified for the duration of all threads. The application will be running on Linux and Windows and is written in C++ if that makes any difference.
回答1:
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.
回答2:
If the data is truly read-only for the duration of the multi-threaded access, then no synchronization is necessary.
回答3:
Yes, that's fine.
You shouldn't have any problem.
回答4:
If the data is fixed before any of the reading threads start, then yes, it's OK.
来源:https://stackoverflow.com/questions/5643060/is-it-wise-to-access-read-only-data-from-multiple-threads-simultaneously