none
concreate RRS feed

  • Question

  • Can any body tell me what are the main difference between spinlock  and mutex ?

    when should we use spinlock and mutex  semaphore ?

    BR

    Thursday, August 29, 2013 1:03 PM

Answers

  • Both of these provide mutual exclusion, the difference is in the implementaion:

    Spinlocks are so named since the thread calling them spins on the code waiting for the lock to become available.  They are extremely fast to take and release, which makes them good for protecting data that is accessed with a short piece of code.  They raise IRPQ to DISPATCH_LEVEL which means no other thread is running on the processor.  So if you take a spinlock, you first can only issue a limited set of calls (because of the IRQL) and if another processor trys to acquire the spinlock that processor is not able to run threads while waiting for the owner to complete.  This later characteristic is why it is best if you don't hold a spinlock very long.  Finally, there is a variant of spinlocks for interrupt handling, these are the only locks that can protect against accesses involving data used by the interrupt service routines (note: these do not run at DISPACTH_LEVEL but at the IRQL of the interrupt).

    Kernel mutexs are standard dispacth objects, so they don't raise the IRQL to DISPATCH (though some do go to APC_LEVEL) and if you wait on the mutex another thread can be scheduled on the processor.  They have significantly more overhead in acquistion and release code than spin locks, so are better for data that will be accessed for a long time.

    One of the great things about Windows is the large number of synchronization mechanisms that it provides.  It takes some time to learn them all, but they provide a lot of flexibility for the developer.


    Don Burn Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr

    Thursday, August 29, 2013 1:15 PM

All replies

  • sorry , by mistake  I put wrong subject....

    Thursday, August 29, 2013 1:05 PM
  • Both of these provide mutual exclusion, the difference is in the implementaion:

    Spinlocks are so named since the thread calling them spins on the code waiting for the lock to become available.  They are extremely fast to take and release, which makes them good for protecting data that is accessed with a short piece of code.  They raise IRPQ to DISPATCH_LEVEL which means no other thread is running on the processor.  So if you take a spinlock, you first can only issue a limited set of calls (because of the IRQL) and if another processor trys to acquire the spinlock that processor is not able to run threads while waiting for the owner to complete.  This later characteristic is why it is best if you don't hold a spinlock very long.  Finally, there is a variant of spinlocks for interrupt handling, these are the only locks that can protect against accesses involving data used by the interrupt service routines (note: these do not run at DISPACTH_LEVEL but at the IRQL of the interrupt).

    Kernel mutexs are standard dispacth objects, so they don't raise the IRQL to DISPATCH (though some do go to APC_LEVEL) and if you wait on the mutex another thread can be scheduled on the processor.  They have significantly more overhead in acquistion and release code than spin locks, so are better for data that will be accessed for a long time.

    One of the great things about Windows is the large number of synchronization mechanisms that it provides.  It takes some time to learn them all, but they provide a lot of flexibility for the developer.


    Don Burn Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr

    Thursday, August 29, 2013 1:15 PM
  • Hi,

    You can always read it here in this old but valid article: http://www.osronline.com/article.cfm?id=93

    Regards,

    Alon

    Thursday, August 29, 2013 1:45 PM
  • While that is a good article, the number and types of locks have changed greatly since it was published.


    Don Burn Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr

    Thursday, August 29, 2013 1:49 PM
  • I agree there are some enhancements since (like queued spin locks) but the fundamentals remains the same J

    Thursday, August 29, 2013 2:05 PM
  • Thanks ..seems to be a good article....
    Thursday, August 29, 2013 2:47 PM
  • What is the best way to synchronize between ISR routine and DPC routine ?
    Friday, August 30, 2013 3:18 AM
  • The only is the interrupt spin lock, technically you can also use interlocked operations, but that only works for very simple scenarios

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Friday, August 30, 2013 4:56 AM