How do you use std::atomic to achieve thread-safety without locking mutexes?

426 views Asked by At

I know, that in some situations you can avoid having to lock mutexes (std::mutex) by using std::atomic, thus increasing performance.

Can you name a situation like this, and preferably show some example code on how to do this (how do you use std::atomic)?

Also, when I lock a mutex, the performance degrades because other threads are unable to continue doing their work for the period of time the mutex is locked. Is this the only issue with a mutex? I mean, is locking/unlocking a mutex an expensive operation or it's just about what I mentioned above?

0

There are 0 answers