本文发表在 rolia.net 枫下论坛Ask yourself some question?
1. will it sync with multi-thread in one process, or sync between multi-processes?
2. will it allow re-entry?
3. what kind of wake up policy you want? like spinlock is user level, it keep on checking until it is available(don't sleep and wait kernel to wake it up)
CriticalSection
* Fast mixed user/kernel mode execution.
* Synchronizes threads within a single process.
* Does not return until the object is owned.
* Allows recursion.
* Efficiently waits ( vs spinlock)
MutexSem?
* Not as fast execution. May/must transition to kernel mode.
* Synchronizes threads in multiple processes on a single machine.
* Does not return until the object is owned or a given timeout value is reached.
* Allows recursion.
* Can be used in multiple wait situations.
* Efficiently waits ( vs spinlock)
the difference between mutex and semaphore:
1. binary semaphore can be used as mutex, counting semaphore is different
2. if you have two concurrent threads, with mutexes you will always have a pair of get/put mutex in each thread. With semaphores, you may wait (get) for the semaphore in one thread and put the semaphore in the other thread. But Never use like this with mutexes
Spinlock
* Fast user mode execution.
* Synchronizes threads within a single process, or multiple processes if in shared memory.
* Does not return until the object is owned.
* Does not support recursion.
* Consumes 100% of CPU while "waiting".
About the event, the purpose using that is different:
A event would be used when you want to awaken one or more threads when an action need be performed, an event is generally used in situations where one thread performs some initialization work, and then signals other threads to perform rest of the work. On the other hand, a mutex is generally used when we need to safeguard a common resource against simultaneous read/write operations from multiple threads.更多精彩文章及讨论,请光临枫下论坛 rolia.net
1. will it sync with multi-thread in one process, or sync between multi-processes?
2. will it allow re-entry?
3. what kind of wake up policy you want? like spinlock is user level, it keep on checking until it is available(don't sleep and wait kernel to wake it up)
CriticalSection
* Fast mixed user/kernel mode execution.
* Synchronizes threads within a single process.
* Does not return until the object is owned.
* Allows recursion.
* Efficiently waits ( vs spinlock)
MutexSem?
* Not as fast execution. May/must transition to kernel mode.
* Synchronizes threads in multiple processes on a single machine.
* Does not return until the object is owned or a given timeout value is reached.
* Allows recursion.
* Can be used in multiple wait situations.
* Efficiently waits ( vs spinlock)
the difference between mutex and semaphore:
1. binary semaphore can be used as mutex, counting semaphore is different
2. if you have two concurrent threads, with mutexes you will always have a pair of get/put mutex in each thread. With semaphores, you may wait (get) for the semaphore in one thread and put the semaphore in the other thread. But Never use like this with mutexes
Spinlock
* Fast user mode execution.
* Synchronizes threads within a single process, or multiple processes if in shared memory.
* Does not return until the object is owned.
* Does not support recursion.
* Consumes 100% of CPU while "waiting".
About the event, the purpose using that is different:
A event would be used when you want to awaken one or more threads when an action need be performed, an event is generally used in situations where one thread performs some initialization work, and then signals other threads to perform rest of the work. On the other hand, a mutex is generally used when we need to safeguard a common resource against simultaneous read/write operations from multiple threads.更多精彩文章及讨论,请光临枫下论坛 rolia.net