c:c_threads:protected_access_to_shared_data_or_shared_resources
This is an old revision of the document!
C - C++ Threads - Protected access to shared data or shared resources
In a multi-threaded environment, more than one thread is often competing for a resource or shared data.
This often results in undefined behavior for the resource or data, unless the resource or data is protected using some mechanics that only allows ONE thread to act on it at a time.
There are different methods that can be used for this protection:
- Mutex: The classical way would be to protect access to a variable by a mutex.
- Writelock / Readlock: Usually cheaper than mutexes.
- Atomic Read & Write: Since C++ 11. Also called lockless. Atomic is here to ensure no races are to be expected while accessing a variable.
c/c_threads/protected_access_to_shared_data_or_shared_resources.1623143112.txt.gz · Last modified: 2021/06/08 09:05 by peter