同时从多个线程访问只读数据是否明智?
我有一个正在尝试使其成为多线程的应用程序。每个线程都会访问一大块只读数据。
如果多个线程同时访问数据可以吗?我知道,如果数据不是只读的,我将需要使用互斥体或其他形式的同步来防止竞争条件。但我想知道是否可以读取数据而不考虑同步。
在所有线程的持续时间内,相关数据不会被修改。该应用程序将在 Linux 和 Windows 上运行,并且是用 C++ 编写的(如果有什么不同的话)。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果数据在读取它的所有线程的生命周期内都是只读的,那么是的,在没有同步的情况下读取数据是完全可以的。
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.
如果数据在多线程访问期间确实是只读的,则不需要同步。
If the data is truly read-only for the duration of the multi-threaded access, then no synchronization is necessary.
是的,没关系。
你应该没有任何问题。
Yes, that's fine.
You shouldn't have any problem.
如果数据在任何读取线程启动之前已修复,那么是的,没问题。
If the data is fixed before any of the reading threads start, then yes, it's OK.