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. Database & SysAdmin
  3. System Admin
  4. Mutex and Semaphore?

Mutex and Semaphore?

Scheduled Pinned Locked Moved System Admin
question
5 Posts 4 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.
  • B Offline
    B Offline
    badal_akr
    wrote on last edited by
    #1

    What is the difference between mutex and semaphore and what are their applications? Anil Kumar

    M R A B 4 Replies Last reply
    0
    • B badal_akr

      What is the difference between mutex and semaphore and what are their applications? Anil Kumar

      M Offline
      M Offline
      Marc Soleda
      wrote on last edited by
      #2

      Basically a semaphore is an integer counter and a mutex it's a binary semaphore: it's a semaphore of max count 1. They are restricted variables used in multi threaded programming environment to control the access to shared resources. By increasing or decreasing its value each thread knows if the resources are available or not. Marc Soleda. ... she said you are the perfect stranger she said baby let's keep it like this... Tunnel of Love, Dire Straits.

      1 Reply Last reply
      0
      • B badal_akr

        What is the difference between mutex and semaphore and what are their applications? Anil Kumar

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

        The classical example is to synchronize shared memory access between multiple threads. A mutex can only be owned by a single thread at a time. Multiple semaphores can be created. In memory access, multiple readers are allowed but only one writer. The two are used in conjunction. Study the following sequences until you understand how both readers and writers interact to synchronize thread safe memory access. Before reading shared memory, a thread must execute the following sequence: 1) Acquire the mutex. If the mutex is not available, another thread is writing memory, and the reader must wait for the mutex. 2) After acquiring mutex, the reader knows it is "safe", i.e no other thread is writing to memory. 3) Acquire a semaphore. This is a signal that other threads may check to see if another thread is currently reading memory. 4) Release the mutex so the next reader thread may acquire the mutex just long enough to acquire a semaphore. 5) Read the shared memory. 6) After memory read is complete, release the semaphore, to signal that the thread is no longer reading the memory. Before writing shared memory, a thread must execute the following sequence: 1) Acquire the mutex. If the mutex is not available, another thread is writing memory, and the second writer must wait for the mutex. Once a writer acquires the mutex, no other threads can access the memory for read or write, because both operations require the mutex, which is now locked by the writer. 2) Wait for all semaphores to be released. Threads that started reading the memory before the writer locked the mutex may not be finished yet. Once all semaphores have been released, the writer knows that no other threads are accessing the memory. 3) Write to the shared memory. 4) Release the mutex. Now other threads may acquire the mutex long enough to also acquire a semaphore, and start reading the memory again. Or another thread may acquire the mutex for a write operation.

        1 Reply Last reply
        0
        • B badal_akr

          What is the difference between mutex and semaphore and what are their applications? Anil Kumar

          A Offline
          A Offline
          Andy Brummer
          wrote on last edited by
          #4

          Mutex and sempaphores are generic terms for thread locking objects. These are the definitions used by the Microsoft handle objects. Mutexes and Semaphores are similar. Licke marcdev said, you can set the max count lock count on a semaphore. A mutex behaves similarly to a single count semaphore, except for relocking the mutex by a thread. The thread that owns a mutex can lock it multiple times without stopping and it will allow another thread to own it after it unlocks it once for each time that it locked it.

          1 Reply Last reply
          0
          • B badal_akr

            What is the difference between mutex and semaphore and what are their applications? Anil Kumar

            B Offline
            B Offline
            badal_akr
            wrote on last edited by
            #5

            Thanks for all i got the difference. Regards Anil Kumar

            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