Threads and simple Dead lock cure

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

    One way to ensure the ordering that other folks have talked about is to acquire locks in an order defined by their memory address. If at any point, you try to acquire a lock that should have been earlier in the sequence, you release all the locks and start over.

    With a little work, it's possible to do this nearly automatically with some wrapper classes around the system primitives.

提交回复
热议问题