User Tools

Site Tools


c:c_threads:mutex:do_not_use_a_mutex_when_std:atomic_types_will_suffice

C - C++ Threads - mutex - Do not use a mutex when std::atomic types will suffice

When using simple data types that needs to be updated, for example, a simple bool or a integer counter, using std::atomic will almost yield better performance than using a mutex.

For example, instead of doing :

int counter;
....
mu.lock();
counter++;
mu.unlock();

Try:

std::atomic<int> counter;
...
counter++;

References

c/c_threads/mutex/do_not_use_a_mutex_when_std/atomic_types_will_suffice.txt · Last modified: 2021/06/08 09:22 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki