Mutual exclusion
Mutual exclusion mechanisms ensure that only one execution thread can access a resource at the same time.
#Header
#include <osbs/bmutex.h>
#Functions
| Mutex* | bmutex_create (void) |
| void | bmutex_close (...) |
| void | bmutex_lock (...) |
| void | bmutex_unlock (...) |
In processes with multiple threads, mutual exclusion guarantees that only one of them can execute a critical section at a specific moment of time. The critical section is a block of code that normally protects a shared resource that does not support concurrent access.
- Use bmutex_create to create a lock.
- Use bmutex_lock to lock a critical section.
- Use bmutex_unlock to unlock a critical section.
#1. Locks
Locks or Mutex are synchronization objects managed by the operating system that mark the beginning and end of a critical section (Figure 1). When a thread is going to access a certain share, you must call the method bmutex_lock to guarantee exclusive access. If another thread is using the resource (it has previously called bmutex_lock), the current thread will stop until the resource is released through bmutex_unlock. Blocking and unblocking threads is handled by the operating system itself. The programmer should only worry about identifying and protecting the critical sections. Multi-thread example.
#bmutex_create ()
Creates a mutual exclusion object that allows multiple threads to share the same resource, such as a memory or file area on disk, preventing them from accessing at the same time.
Mutex* bmutex_create(void);
Return
The mutual exclusion handler.
Remarks
Threads, Multi-thread example.
#bmutex_close ()
Close the mutual exclusion object and free memory.
void bmutex_close(Mutex **mutex);
| mutex | The mutual exclusion handler. It will be set to |
Remarks
Threads, Multi-thread example.
#bmutex_lock ()
Marks the start of a critical section, blocking access to a shared resource. If another thread tries to block, it will be stopped until the current thread calls bmutex_unlock.
void bmutex_lock(Mutex *mutex);
| mutex | The mutual exclusion handler. |
Remarks
Threads, Multi-thread example.
#bmutex_unlock ()
Mark the end of a critical section, unlocking access to a shared resource. If another thread is waiting, access will be allowed to its critical section and, therefore, to the shared resource.
void bmutex_unlock(Mutex *mutex);
| mutex | The mutual exclusion handler. |
Remarks
To avoid unnecessary delays, the time between bmutex_lock and bmutex_unlock should be as short as possible. Any calculation that the thread can make in its private memory space must precede the call to bmutex_lock. Threads, Multi-thread example.


