How to avoid deadlocks?

前端 未结 8 879
隐瞒了意图╮
隐瞒了意图╮ 2021-02-01 03:30

When using multiple threads, shared memory needs to be locked by critical sections. However, using critical sections causes potential deadlocks. How can they be avoided?

8条回答
  •  时光说笑
    2021-02-01 03:52

    You can avoid critical sections by using message passing instead (synchronous and asynchronous calls). When using synchronous calls, you still have to make sure not to make a circular call, in which thread A asks thread B a question, and B needs to ask A a question to be able to respond.

    Another option is to make asynchronous calls instead. However, it is more difficult to get return values.

    Note: Indeed, a message passing system is implemented using a critical section that locks the call queue, but it is abstracted away.

提交回复
热议问题