0

So, last week, I was researching about Mutex and Semaphores. I came to find this post which really helped me figure out what Semaphores are. Now that I understand what the theorical difference is, between Binary Semaphore and Mutex, I still wonder how to actually use them. I'm currently using C# and I can't find any way to use a Binary Semaphore in this language.

Can someone please post any (simple) code example on how to use Binary Semaphore vs Mutex ? Any widely used language would do the job. You can even post a Powershell/Bash script.

AniMir
  • 150
  • 12
  • You question title suggests you want to know about differences between them, but the body of your question asks for code examples. So what do you need? –  Dec 11 '19 at 21:22
  • I want to know the actual (as opposed to theorical) differences between the two. Therefore, showing a piece of code would be worth a thousand words. Though, documentation on both mutex and binary semaphore provided by a same OS/language would be relevant too... – AniMir Dec 12 '19 at 06:26

1 Answers1

1

A very coarse approximation is if your thread of execution needs to block while holding a resource (ie. mutex, sem), you shouldn't use a mutex. The problem with that approximation is that block means something different in a UI program and an interrupt handler; blocking is a relative notion.

Mutexes are bound to owners; Semaphores are not. The only agent that can release a mutex is the one that acquired it. Semaphores do not have this limitation. Because of this limitation, if a low priority agent blocks a high priority agent on a mutex, the supervisor (kernel, whatever..) can boost the priority of the owner until it relinquishes it. This can be applied transitively to solve non-cyclic priority inversion. You cannot do this with semaphores, since they lack the concept of an owner.

For example, thread 1 can acquire a semaphore, thread 2 can wait on it, and thread 3 can relinquish it. That might sound like chaos, but it might be the foundation of a more sophisticated system where thread 3 and 1 are communicating via some means, so thread 1 can hand ownership of a resource over to thread 3 directly. This can't be done with mutexes.

That said; I could like hack a pthread_mutex_transfer(mutex, pthread) into any existing implementation, thumbing my nose at the purists and theoreticians.

mevets
  • 10,070
  • 1
  • 21
  • 33
  • 1
    "_you shouldn't use a mutex_" why not? – curiousguy Dec 12 '19 at 01:50
  • re: coarse approximation. Consider synchronizing with an interrupt handler. If you block holding a mutex, say waiting for an event from a different device, you have now bound the interrupting device to the signalling one. Furthermore, the interrupting device may cause other devices interrupts to be delayed ( a not uncommon interrupt structure ), including the one you are blocked on. Even cycle-detection would likely miss this, since part of the cycle is hidden in some exterior bus device. – mevets Dec 12 '19 at 11:11
  • You can construct similar things with UI-type code where a dialog goes to sleep holding say a file access mutex, while another part of the program wants to auto-save, but is blocked by the file access mutex. It makes programs more sluggish and quirky than their design frameworks. – mevets Dec 12 '19 at 11:17