简体   繁体   English

共享内存C ++读写同步

[英]Shared memory C++ Read and Write synchronisation

Will the shared memory be corrupted if two programs access it simultaneously, one writing into it and the other one reading from it? 如果两个程序同时访问共享内存,一个程序写入共享内存,另一个程序读取共享内存,共享内存会被破坏吗?

I have two programs, one will get some data from servers and web and save parsed data into the shared memory, and i have a read program, that will read until the last saved data set. 我有两个程序,一个程序将从服务器和Web获取一些数据并将解析的数据保存到共享内存中,而我有一个读取程序,该程序将一直读取到最后保存的数据集。

For example, if the first program has collected data from 100 servers and is currently in the 101th server, all the data until the 100th server will be read by the reader program. 例如,如果第一个程序已从100个服务器收集了数据并且当前在第101个服务器中,则读取器程序将读取直到第100个服务器的所有数据。 Once 101th is finished, then reader program will read the 101th data set. 第101个数据完成后,读取器程序将读取第101个数据集。 Here data set from a server may have multiple data, like disk space, load etc. So does this kind of access corrupt the data in the shared memory? 这里来自服务器的数据集可能具有多个数据,例如磁盘空间,负载等。那么这种访问是否会破坏共享内存中的数据? Or is it ok the way i do it? 还是我可以这样做?

Is there a reason you're doing this with processes instead of threads? 您使用进程而不是线程执行此操作是有原因的吗? That might make your life harder from a synchronization perspective. 从同步的角度来看,这可能会使您的生活更加艰难。

If you were working with threads I would tell you to protect your accesses with a semaphore so that you can guarantee that the reader is not reading the same data set that the writer is writing; 如果您使用的是线程,我会告诉您使用信号量来保护您的访问,以便可以保证读取器不会读取与写入器相同的数据集。 I believe that implementations of semaphores and other synchronization primitives exist for inter-process shared memory as well but that's a little less common. 我相信也存在进程间共享内存的信号量和其他同步原语的实现,但是这种情况不太常见。

The reason you should definitely be using someone else's synchronization primitives is that on modern hardware, writes can be reordered or delayed eg the first program is "currently in the 101st server" but in actuality the writes to the data for the 100th server are not yet written back from the CPU. 您绝对应该使用其他人的同步原语的原因是,在现代硬件上,写入可以重新排序或延迟,例如第一个程序“当前在第101个服务器中”,但实际上第100个服务器的数据尚未写入从CPU写回。 A semaphore or mutex will include the appropriate memory fencing instructions to flush writes so that memory looks consistent from other threads/processes; 信号量或互斥量将包括适当的内存隔离指令以刷新写入,以便使内存与其他线程/进程保持一致; you should absolutely use someone else's carefully written implementation rather than write your own synchronization logic. 您应该绝对使用别人精心编写的实现,而不是编写自己的同步逻辑。

Also, when you are testing it is very important that you test on a multiprocessor machine, as race conditions between processes are much less likely to appear on a uniprocessor box. 另外,在进行测试时,在多处理器计算机上进行测试非常重要,因为进程之间的竞争条件不太可能出现在单处理器计算机上。

What you have described is actually a common computing problem in concurrency called Readers-writers 您所描述的实际上是并发中的一个常见计算问题,称为“ 读写器”

If you try to read from the memory while other program is writing to it, you will most likely get corrupted data. 如果尝试在其他程序正在向其写入数据时从内存中读取数据,则很可能会损坏数据。 You should use one of synchronization primitives (locks, semaphores, monitors...) to ensure this situation will never happen. 您应该使用一种同步原语(锁,信号量,监视器...)来确保这种情况永远不会发生。

I recommend you to have a look at The Little Book of Semaphores , especially chapter 4.2 Readers-writers problem . 我建议您看一下《信号量小书》 ,特别是第4.2章“ 读写者问题”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM