Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A RW lock for c++11 threads [duplicate]

Tags:

I'd like to use the new standard threads instead of boost:threads but I've noticed the old shared_mutex is not available. What would be a good recommendation to replace this functionality and give me a multiple-readers, single-writer lock?

like image 206
gbjbaanb Avatar asked May 27 '13 13:05

gbjbaanb


People also ask

What is read/write lock C++?

With C++14 came reader-writer locks. The idea is straightforward and promising. Arbitrary reading threads can access the critical region at the same time, but only one thread is allowed to write.

How is Rwlock implemented?

Instead of having a single lock method, they have two - one for readers and one for writers. When readers enter the critical section they invoke the reader lock (and then reader unlock on exit); when writers enter the critical section they invoke the writer lock (and then writer unlock on exit).

When to use shared_ mutex?

Shared mutexes are usually used in situations when multiple readers can access the same resource at the same time without causing data races, but only one writer can do so.

What is std:: shared_ mutex?

std::shared_mutex The shared_mutex class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads.


1 Answers

std::shared_mutex will be part of the C++14 Standard Library. It did not make it to C++11 just because there was no time to formulate a proposal and discuss it thoroughly.

You can still use boost::shared_mutex though. Under Windows, if you are working with Windows Vista or later, you can use Slim Read-Write Locks, which are optimized for speed and memory consumption.

like image 108
Andy Prowl Avatar answered Oct 05 '22 17:10

Andy Prowl