0
Multiple read one write
Hi Please refer code below: I hope It is proper read write lock meaning one exclusive write lock where as shared read locks. Let me know if it is not so in shared code. Query is related to implementation. What does this guarantee? Shared mutex or shared lock ? https://sololearn.com/compiler-playground/c8clbCkBaUxf/?ref=app
4 Antworten
+ 1
Your code incorrectly use shared_mutex which can lead to undefined and does not properly implement a read-lock because In the read() function, you're using lock_guard<shared_mutex> which only locks the shared_mutex and prevents other threads (readers or writers) from accessing the resource simultaneously, defeating the purpose of shared locks.
I think you should use shared_lock for readers and unique_lock for writers
+ 1
When I tried your code on msvc 2022, I found out the behavior was undefined compared to when I tried it on clang15 and g++14.
I figure it out experimentally
0
Thanks for correcting. Updated code.
Why it would be undefined behaviour with read operation having lock guard and write having shared lock?
Isn't that lead to data race and two threads may corrupt the data while writing . So, data race is not a avoided but behaviour is not dead lock or undefined. Correct or am I missing something 🤔?
0
How you come to conclusion about undefined behaviour? Just curious to know