To Mutex or Not To Mutex?

后端 未结 5 2105
眼角桃花
眼角桃花 2020-12-21 00:43

Do I need a mutex if I have only one reader and one writer? The reader takes the next command (food.front()) from the queue and executes a task based on the com

5条回答
  •  臣服心动
    2020-12-21 01:23

    your question depends on two conditions:

    1. there are only two threads, one is producer, the other is consumer
    2. the structure is designed for lock-free

    if satisfy both, you can drop the lock, or you need to use a lock to protect the queue structure.
    for dropping the lock, must remember to update the header or tailer pointer in the end of steps.

提交回复
热议问题