Performance difference between IPC shared memory and threads memory

后端 未结 5 1708
孤独总比滥情好
孤独总比滥情好 2020-12-08 04:54

I hear frequently that accessing a shared memory segment between processes has no performance penalty compared to accessing process memory between threads. In other words, a

5条回答
  •  暖寄归人
    2020-12-08 05:37

    Beside the costs for attaching (shmat) and detaching (shmdt) of the shared memory, access should be equally fast. In other words, it should be fast as the hardware supports it. There should be no overhead in form of an extra layer for each access.

    Synchronization should be equally fast, too. For example, in Linux a futex can be used for both processes and threads. Atomic variable should also work fine.

    As long as the attaching/detaching costs do not dominate, there should be no disadvantage for using processes. Threads are simpler, however, and if your processes are mostly short-lifed, the attaching/detaching overhead might be an issue. But as the costs to create the processes will be high, anyway, this should not be a likely scenario if you are concerned about performance.

    Finally, this discussion might be interesting: Are shmat and shmdt expensive?. (Caveat: It is quite outdated. I don't know if the situation has changed since.)

    This related question could also be helpful: What's the difference between shared memory for IPCs and threads' shared memory? (The short answer: Not much.)

提交回复
热议问题