-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
read-write lock with writer-preference #1553
Conversation
C++ std::shared_mutex may lead to writer starvation with multiple highly concurrent readers, and it doesn't provide API to configure priority. Luckily, with Pthread implementation, we may configure writer preference. It expect that write operations are rare, compared to read ones.
|
For this case you could use the "no lock concept", using an atomic operation on int, as follows:
Using this algorithm the writer always takes priority and you don't need mutex. The performance is therefore much better. edit: I will post a sample implementation of ReadWriteLock using this algo. |
but this is spinlock - it is good for write operations that are really fast, but in our case, it is better to block the thread I believe... |
I prepares a PR with the sample. Currently I made that thread writer must wait all readers have freed the lock. So One question, is a writer must wait all reader are freed ? |
Yes, because readers are expecting that structures will not be modified by writer when they are holding the lock... |
Karry, I made the PR for your branch. |
@Karry : This one is now obsolete? |
C++ std::shared_mutex may lead to writer starvation with multiple highly concurrent readers, and it doesn't provide API to configure priority. Luckily, with Pthread implementation, we may configure writer preference. It expect that write operations are rare, compared to read ones.
This problem (writer starvation) is visible during navigation, when multiple threads reads database with every position update (renderer thread and navigation thread). When you drive to the tunnel and application change style to nigh one (daylight flag setup to false), it may take several minutes to change map style - because style-sheet change requires write lock to databases...