Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows API
  4. spinlock mutex usage examples

spinlock mutex usage examples

Scheduled Pinned Locked Moved Windows API
questionlounge
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rupeshkp728
    wrote on last edited by
    #1

    Can anybody give some general examples of usage of spinlock where mutexes cannot be used? Also whats the difference between the spinlock and mutex?

    C 1 Reply Last reply
    0
    • R rupeshkp728

      Can anybody give some general examples of usage of spinlock where mutexes cannot be used? Also whats the difference between the spinlock and mutex?

      C Offline
      C Offline
      Covean
      wrote on last edited by
      #2

      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

      R 1 Reply Last reply
      0
      • C 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

        R Offline
        R Offline
        rupeshkp728
        wrote on last edited by
        #3

        Thanks a lot Covean for the reply. Any other exapmle ohter then a DMA.

        C 1 Reply Last reply
        0
        • R rupeshkp728

          Thanks a lot Covean for the reply. Any other exapmle ohter then a DMA.

          C Offline
          C Offline
          Covean
          wrote on last edited by
          #4

          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

          R 1 Reply Last reply
          0
          • C 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

            R Offline
            R Offline
            rupeshkp728
            wrote on last edited by
            #5

            Thanks Covean.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups