Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
UppHub
Status & Roadmap
FAQ
Authors & License
Forums
Funding U++
Search on this site











SourceForge.net Logo

SourceForge.net Logo

GitHub Logo

Discord Logo

Mutex

 

class Mutex : private NoCopy

This well know synchronization primitive is usually used to serialize access to shared data or other shared resources. Only single thread can execute the code between calls to Enter and Leave. If any other thread attempts to Enter the protected section while other thread is performing it, it is blocked until performing thread indicates leaving the section by invoking Leave. Mutex is reentrant (same thread can Enter the Mutex multiple times).

 

 

Public Method List

 

void Enter()

Enter the protected region of code. Only single thread can enter the region, other threads block until Leave is invoked.

 


 

bool TryEnter()

Attempts to enter the protected region of code. If other thread owns the region, returns false, otherwise gains the ownership of Mutex and returns true. Never blocks.

 


 

void Leave()

Leave the protected region of code. Only the same thread that called Enter can invoke Leave.

 

 

 

 

StaticMutex

 

class StaticMutex

Variant of Mutex that can be used as static or global variable without the need of initialization  - it has no constructor and correctly performs the first initialization when any of methods is called. That avoids problems with initialization order or multithreaded initialization issues.

 

 

Public Method List

 

Mutex& Get()

operator Mutex&()

Returns the initialized instance of Mutex.

 


 

bool TryEnter()

void Enter()

void Leave()

Calls respective methods of Mutex instance.

 

 

 

 

Mutex::Lock

 

class Mutex::Lock

This nested class automates calls to Mutex::Enter / Mutex::Leave for block of code using C++ constructor / destructor rules. Using operator StaticMutex::Mutex, it can be used with StaticMutex as well.

 

 

Constructor / Destructor detail

 

Lock(Mutex& s)

Performs s.Enter().

 


 

~Lock()

Performs s.Leave() where s is the constructor parameter.

 

 

Do you want to contribute?