difference between message queue and shared memory?

后端 未结 4 1363
挽巷
挽巷 2021-02-01 08:30

I read a lot of articles about differences between message queue and shared memory. But still not clear which one is good for achieving good performance.

Like shared mem

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-01 09:21

    Message queue has inherent synchronization overhead, guarantee of safety at cost of performance. Shared memory has no safeguards - if two threads access it simultaneously, they will possibly conflict (write inconsistent data) unless you assure thread safety yourself. This may be insignificant in case minor errors are allowed (say, the data goes to analog output and some noise is acceptable), so you can skip error checking altogether, and go along with "good enough" approach at quite high performance gain. Also, shared memory allows for exchange of big pieces of data and common and persistent storage of data common to several apps saving memory storage. Message queues are for lower throughput - you can for example utilize them for safeguarding access to shared memory.

提交回复
热议问题