0

When I searched how I can use semaphores in C++11, I saw people often suggest that I make one out of a std::mutex and a std::condition_variable (this post for example). This made me think that semaphores are a higher-level abstraction than mutexes and condition variables.

However, after I took our operation system class, I now know that in the kernel, semaphores are usually the lowest-level of abstraction. Semaphores are implemented by disabling interrupts, and locks are essentially semaphores with value 1, while condition variables are implemented from ground up without using semaphores or locks. So it seems semaphores (at kernel-level) are not in anyway a higher-level abstraction than locks or condition variables.

So my question is, is my conclusion that "semaphores (in C++11) are a higher-level abstraction" just an artifact of the limitation of the standard library? Or is it a result of the difference between user-level and kernel-level synchronization?

Community
  • 1
  • 1
Zizheng Tai
  • 6,170
  • 28
  • 79
  • "This made me think" - why? Neither is a higher level of abstraction. If anything a Mutex is a special case of semaphore if you want to insist on a relationship. – kabanus Dec 19 '16 at 08:14
  • @kabanus Well because when you build something out of something else, it usually implies the former is a higher-level abstraction than the latter is...? – Zizheng Tai Dec 19 '16 at 08:16
  • Not necessarily. Note you can build a mutex from a semaphore (set count=1) and vice-versa as you posted. A 'mutex' is smaller in a sense, but I think it's a bit hard to define one being a higher abstraction than another. A higher abstraction to both could be a general 'lock' for example, defining the general rules for locking but not guaranteeing an implementation. The choice of implementing a mutex in C++ and not a semaphore is, in my opinion, arbitrary. There are non-standard libraries with semaphores. – kabanus Dec 19 '16 at 08:22
  • There is no factual answer. "Higher level of abstraction" is not a technical term with a generally accepted definition. I have at various times implemented a condition variable type using semaphores, and a semaphore type using condition variables. I do not see this question going anywhere useful. – Jive Dadson Dec 19 '16 at 09:00

1 Answers1

2

It is a result of the difference between user-level and kernel-level synchronization.

When you state in the kernel, you do not state which kernel you refer to. The standard library must be as agnostic as it can regarding the kernel, thus choosing the most common interface. Not all kernels implement locks the exact same way.

The implementation of locks in the Linux kernel, for instance, is quite more subtle than a basic semaphore initialized at value 1. Maybe you heard about futexes.

Finally, since semaphores usages are quite scarce compared to that of mutexes, it makes sense to choose mutexes as the common interface for the standard library.

Jean-Bernard Jansen
  • 7,544
  • 2
  • 20
  • 17