Blog Archive

Semaphore, Mutex, Conditional variable, Spin lock

Difference between mutex and semaphore
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A mutex has two possible states: unlocked (not owned by any thread), and locked (owned by one thread). A mutex can never be owned by two different threads simultaneously. A thread attempting to lock a mutex that is already locked by another thread is suspended until the owning thread unlocks the mutex first.

Example -
A shared global variable x can be protected by a mutex as follows:

int x;
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;

All accesses and modifications to x should be bracketed by calls to pthread_mutex_lock and pthread_mutex_unlock as follows:

pthread_mutex_lock(&mut);
/* operate on x */
pthread_mutex_unlock(&mut);



TBD


Is it possible to implement semaphores without special machine instructions(compare and swap instruction(IBM 370) or atomic read
and clear instruction(AT &T 3B20))?

YES - Bach Chapter 12 Multiprocessor Systems

No comments: