difference between message queue and shared memory?

后端 未结 4 1319
挽巷
挽巷 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:09

    When using shared memory with consideration for possible race-conditions where one process writes to it and another reads from it, something to bear in mind. There is an associated risk of using the former, suppose two processes are using it, one to write to it, the other to read from it, the one that is writing dies due to abnormal condition, the process reading it could hang or crash.

    Shared memory can be deemed as faster (low overhead, high volume of data passing) then queues. But queues on the other hand, requires high overhead (the set up for making a queue to be permanent etc) with low volume of data.

    The onus with shared memory is that you have to implement synchronization in order to be thread safe. Have a look at the excellent article by Beej on IPC.

    When using Queues, they are thread-safe, and not alone that, messages are held in the queue regardless of the outcome, suppose two processes are using the queue, when one process writes to it (in a form of a message) and the other process that is about to read from it gets to die or killed off due to a crash or abnormal condition under a such circumstance, that message is still in place, the other process if restarted can read from the queue, i.e. no data is lost.

    That is the difference between the two.

提交回复
热议问题