SFML 2.5.1

- Main Page
- Related Pages
- Topics
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | List of all members
sf::Mutex Class Reference
System module

Blocks concurrent access to shared resources from multiple threads. More...

`#include <Mutex.hpp>`

Inheritance diagram for sf::Mutex:

## Public Member Functions

| Mutex ()
| Default constructor.
| ~Mutex ()
| Destructor.
void | lock ()
| Lock the mutex.
void | unlock ()
| Unlock the mutex.

## Detailed Description

Blocks concurrent access to shared resources from multiple threads.

Mutex stands for "MUTual EXclusion".

A mutex is a synchronization object, used when multiple threads are involved.

When you want to protect a part of the code from being accessed simultaneously by multiple threads, you typically use a mutex. When a thread is locked by a mutex, any other thread trying to lock it will be blocked until the mutex is released by the thread that locked it. This way, you can allow only one thread at a time to access a critical region of your code.

Usage example:

Database database; // this is a critical resource that needs some protection
sf::Mutex mutex;
void thread1()
{
mutex.lock(); // this call will block the thread if the mutex is already locked by thread2
database.write(...);
mutex.unlock(); // if thread2 was waiting, it will now be unblocked
}
void thread2()
{
mutex.lock(); // this call will block the thread if the mutex is already locked by thread1
database.write(...);
mutex.unlock(); // if thread1 was waiting, it will now be unblocked
}
sf::Mutex
Blocks concurrent access to shared resources from multiple threads.
Definition Mutex.hpp:48
sf::Mutex::lock
void lock()
Lock the mutex.
sf::Mutex::unlock
void unlock()
Unlock the mutex.

Be very careful with mutexes. A bad usage can lead to bad problems, like deadlocks (two threads are waiting for each other and the application is globally stuck).

To make the usage of mutexes more robust, particularly in environments where exceptions can be thrown, you should use the helper class sf::Lock to lock/unlock mutexes.

SFML mutexes are recursive, which means that you can lock a mutex multiple times in the same thread without creating a deadlock. In this case, the first call to lock() behaves as usual, and the following ones have no effect. However, you must call unlock() exactly as many times as you called lock(). If you don't, the mutex won't be released.

See also
  sf::Lock

Definition at line 47 of file Mutex.hpp.

## Constructor & Destructor Documentation

## ◆ Mutex()

sf::Mutex::Mutex | ( |  | ) |

Default constructor.

## ◆ ~Mutex()

sf::Mutex::~Mutex | ( |  | ) |

Destructor.

## Member Function Documentation

## ◆ lock()

void sf::Mutex::lock | ( |  | ) |

Lock the mutex.

If the mutex is already locked in another thread, this call will block the execution until the mutex is released.

See also
  unlock

## ◆ unlock()

void sf::Mutex::unlock | ( |  | ) |

Unlock the mutex.

See also
  lock

The documentation for this class was generated from the following file:

- Mutex.hpp