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

26th Dec 2024, 11:02 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 odpowiedzi
+ 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
26th Dec 2024, 1:09 PM
Badgirl MIRI
Badgirl MIRI - avatar
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 🤔?
26th Dec 2024, 1:22 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
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
26th Dec 2024, 2:01 PM
Badgirl MIRI
Badgirl MIRI - avatar