How pthread_mutex_t is different from semaphore?
The difference between a mutex and a semaphore is how the blocking is handled. Think of a semaphore as simply a counter. This counter is created in the kernel memory space, and is available to be accessed by multiple users and processes. This is useful in that you can have different users or applications, or multiple instances of the same application running that need to access the same resources. When they need to access the resource, the application will need to check the semaphore. The semaphore will be incremented (or decremented depending on the implementation). When the semaphore value reaches zero (usually) the resource is “available” and another process can increment/decrement the semaphore and use the resource, then set the semaphore back. A mutex (mutual exclusion) is generally used to block threads that are initiated by the same calling process. Same principle as a semaphore, but used to protect resources (including variables) in your multi-threaded app. So, for example, if