spinlock mutex usage examples
-
Can anybody give some general examples of usage of spinlock where mutexes cannot be used? Also whats the difference between the spinlock and mutex?
-
Can anybody give some general examples of usage of spinlock where mutexes cannot be used? Also whats the difference between the spinlock and mutex?
Spinlocks are generally used for synchronizing objects within a very very short period of time (in most cases only some micro- or even nanoseconds). Spinlocks consumes cpu power during the wait process (active waiting).
Here some simple example of a spin lock (pseudo-code):
loop:
check if waitobject is set (must be an atomic instruction!)
if set goto exitloop
wait/no operation (on newer cpus there are wait commands with spinlock hint,
that the cpu does not consume that much power in spinlock state.)
goto loop
exitloop:If I'm right spinlocks are normally used to synchronize with very short DMA requests.
Mutexes are also objects to synchronize, but its much slower than a spinlock.
To use an mutex the system has to reserve many system resources. Doing all the
DMA request with mutexes would extremely slow down the system. (I also think its not possible to do that with mutexes.)
But mutexes are very good, if you have to synchronize two processes or something else.In general I would say.
Spinlocks are used to wait for objects that just need a very short time to get in signaled state (>>>1ms).
If something needs more than 1-2ms to get to signaled state you could use critical sections ore mutexes.Greetings Covean
-
Spinlocks are generally used for synchronizing objects within a very very short period of time (in most cases only some micro- or even nanoseconds). Spinlocks consumes cpu power during the wait process (active waiting).
Here some simple example of a spin lock (pseudo-code):
loop:
check if waitobject is set (must be an atomic instruction!)
if set goto exitloop
wait/no operation (on newer cpus there are wait commands with spinlock hint,
that the cpu does not consume that much power in spinlock state.)
goto loop
exitloop:If I'm right spinlocks are normally used to synchronize with very short DMA requests.
Mutexes are also objects to synchronize, but its much slower than a spinlock.
To use an mutex the system has to reserve many system resources. Doing all the
DMA request with mutexes would extremely slow down the system. (I also think its not possible to do that with mutexes.)
But mutexes are very good, if you have to synchronize two processes or something else.In general I would say.
Spinlocks are used to wait for objects that just need a very short time to get in signaled state (>>>1ms).
If something needs more than 1-2ms to get to signaled state you could use critical sections ore mutexes.Greetings Covean
Thanks a lot Covean for the reply. Any other exapmle ohter then a DMA.
-
Thanks a lot Covean for the reply. Any other exapmle ohter then a DMA.
I don't know more examples for this, but in general I would say every time you need a much more higher granularity than 1 ms to sync your objects you should use a spinlock. [edit:] The most spinlocks you will find in the kernel of an os. [/edit]
Greetings Covean
-
I don't know more examples for this, but in general I would say every time you need a much more higher granularity than 1 ms to sync your objects you should use a spinlock. [edit:] The most spinlocks you will find in the kernel of an os. [/edit]
Greetings Covean
Thanks Covean.