Threads and simple Dead lock cure

前端 未结 9 670
余生分开走
余生分开走 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:25

    Try to avoid acquiring one lock and trying to acquire another. This can result into circular dependency and cause for deadlock. If it is un-avoidable then at least the order of acquire locks should be predictable.

    Use RAII ( to make sure lock is release properly in case of exception as well)

提交回复
热议问题