Threads and simple Dead lock cure

前端 未结 9 709
余生分开走
余生分开走 2021-01-04 22:48

When dealing with threads (specifically in C++) using mutex locks and semaphores is there a simple rule of thumb to avoid Dead Locks and have nice clean Synchronization?

9条回答
  •  离开以前
    2021-01-04 23:18

    If you want to attack the possibility of a deadlock you must attack one of the 4 crucial conditions for the existence of a deadlock.

    The 4 conditions for a deadlock are: 1. Mutual Exclusion - only one thread can enter the critical section at a time. 2. Hold and Wait - a thread doesn't release the resources he acquired as long as he didn't finish his job even if other resources are un available. 3. No preemption - A thread doesn't have a priority over other threads. 4. Resource Cycle - There has to be a cycle chain of threads that waits for resources from other threads.

    The easiest condition to attack is the resource cycle by making sure that no cycles are possible.

提交回复
热议问题