Multiple read and single write - threadsafe?

I have a data structure that has two methods of accessing it: query () and modify (). Several threads can use query () at the same time, but only one thread can introduce modification (), and at the same time, all threads using query () must exit before access to the modify () function occurs.

What is the best way to make this data structure unsafe in C ++?

(I read about enhanced read / write locks, but I was told that it could be 3-40 times slower than using mutexes)

+4
source share
1 answer

In general, you should protect shared data with a mutex.

But if the type of shared data is an integer, you can also consider using std::atomic , which is usually much faster than locking a mutex.

+1
source

Source: https://habr.com/ru/post/1439766/


All Articles