linux - 如何选择合适的线程同步机制?
PHPz
PHPz 2017-04-17 13:22:29
0
1
591

众所周知,实现线程进程同步的机制有:临界区、互斥体、信号量、事件、原子操作、自旋锁。除了临界区只能实现线程内部的互斥访问,其他几种机制都可以实现进程间的互斥和同步。那么他们的优劣势是什么?如何在不同应用场景下选择不同的机制?

PHPz
PHPz

学习是最好的投资!

reply all(1)
黄舟

Since you added a Linux tag, I assume you are specifically referring to programs under Linux.

There is no "Critical section/Critical section" and "Event/Event" under Linux, so these two can be eliminated.
Atomic operation/Atomic operation cannot block threads, so it cannot be used if you need to let a thread wait for something. It is only suitable for ensuring data consistency.
Spin lock/spinlock is a lock that "blocks" a thread with a busy loop. It will occupy the CPU during the waiting process, so it is not suitable if you need to let a thread wait for something for a long time. It is only suitable for "blocking" In the event of extremely small probability, because spinlock will not transfer the CPU, if the conditions are almost always met, thread switching can be reduced. In addition, it cannot be used for process synchronization and is only applicable to threads in the same process. Okay, all that's left is the mutex, and you know what it can do.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template