How to avoid deadlocks?

前端 未结 8 948
隐瞒了意图╮
隐瞒了意图╮ 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 04:00

    Among the various methods to enter critical sections -- semaphores and mutexs are the most popular.

    • A semaphore is a waiting mechanism and mutex is a locking mechanism, well the concept is confusing to the most, but in short, a thread activating a mutex can only deactivate it. with this in mind...

    • Dont allow any process to lock partial no of resources, if a process need 5 resources, wait until all the are available.

    • if u use semaphore here, u can unblock/un-wait the resource occupied by other thread. by this i mean pre-emption is another reason.

    These 2 according to me are the basic conditions, the remaining 2 of the common 4 precautions can be related to these.

    If u dont agree ps add comments. I've gtg already late, I will later add a cleaner and clearer explanation.

提交回复
热议问题