Frequency limit of writing to shared memory?

2019-06-06 11:00发布

问题:

Single threaded application (C++) continuously locks, writes and unlocks shared memory - four times a second (the loop is programically set to run once a second and there are 4 writes in the loop and no reads).

EnterCriticalSection(cs);
WriteToSharedMem();
LeaveCriticalSection(cs);

Another application (C) will access this shared memory once every few minutes.

Are there any problems with writing to shared memory at this rate?

Windows XP
C++

回答1:

At this rate there definitely not! This is extremely slow, however I'm not sure Critical section is what you want to use, the way I remember it that only ensures thread safety, not cross-application safety, you should look for something else to lock shared memory. You have to use some Inter-Process Communication (IPC) mechanism to ensure atomic operations with shared memory.



回答2:

The rate you give (four times a second) won't cause a problem, but you can't use critical sections across processes. You need a kernel level synchronization object like a mutex.



回答3:

Not at all. You can get/release the lock thousands (or tens or hundreds of thousands) of times per second. You could easily do a quick benchmark to see.