sf::Mutex Class Reference

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

#include <Mutex.hpp>

Inheritance diagram for sf::Mutex:
sf::NonCopyable

List of all members.

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 thread, 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 db; // 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
     db.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
     db.write(...);
     mutex.Unlock(); // if thread1 was waiting, it will now be unblocked
 }

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 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.

See also:
sf::Lock

Definition at line 47 of file Mutex.hpp.


Constructor & Destructor Documentation

sf::Mutex::Mutex (  ) 

Default constructor.

Definition at line 45 of file Mutex.cpp.

sf::Mutex::~Mutex (  ) 

Destructor.

Definition at line 52 of file Mutex.cpp.


Member Function Documentation

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

Definition at line 59 of file Mutex.cpp.

void sf::Mutex::Unlock (  ) 

Unlock the mutex.

See also:
Lock

Definition at line 66 of file Mutex.cpp.


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