线程间、进程间的同步是通过称为“信号量”signal的一类对象来实现的。下面是一个来自msdn的natice code world的列表:
Name | Relative speed | Cross process | Resource counting | Supported platforms |
Critical Section | Fast | No | No (exclusive access) | 9x/NT/CE |
Mutex | Slow | Yes | No (exclusive access) | 9x/NT/CE |
Semaphore | Slow | Yes | Automatic | 9x/NT |
Event | Slow | Yes | Yes | 9x/NT/CE |
Metered Section | Fast | Yes | Automatic | 9x/NT/CE |
Comments:
1. 如果是进程内同步线程,用critical section,因为他fast。
2. 如果是需要对shared resources做最大值限制,用semaphore。(msdn:A semaphore object is a synchronization object that maintains a count between zero and a specified maximum value. The count is decremented each time a thread completes a wait for the semaphore object and incremented each time a thread releases the semaphore. )
3. interprocess同步,用mutex,sepaphore之类。
4. Event,有两类:manuaset和autoset。可以用来精准控制线程的执行次序。