×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

详细:

本文发表在 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
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / Mutex, Event, critical section,Semaphore. 这么老多,我怎么知道什么情况用哪一个?
    • CriticalSection > Semaphore > Mutex,Event 是特殊的delegate,=member function pointer, 跟前面那三个是两回事。
      • 我没写明白。是C++里跟多线程有关的event, 不是C#里那个。
    • 各有妙用 ;-) 存在基于需求. 要弄明白, 先研究多线程编程的竞争和死锁.
      • 题目是有点大。可有个缺德鬼, 面试问题就这样。
        • 详细:
          本文发表在 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