Questions tagged [rwlock]

Reader-writer Lock

Reader-writer lock is a synchronization mechanism used in concurrent programming to control access to shared resources. It is designed to allow multiple readers to access a shared resource simultaneously while ensuring that only one writer can modify the resource at any given time. When a writer is working on the resource, no readers can access it.

Use Case

Consider a database that is accessed by multiple threads simultaneously. In this scenario, multiple threads may need to read data from the database, but only one thread should be allowed to write data to the database at any given time. If you don't use any synchronization, you will face dirty, non-repeatable and/or phantom reads. And if you use a reader-writer lock, you'll achieve the best performance (using traditional synchronization, where only one thread is allowed to access the database at a given time, would be too strict and hence damaging performance, since it's harmless for the database to be read by multiple readers simultaneously).

Learn More

You can learn more about this synchronization mechanism at the dedicated Wikipedia article.

53 questions
7
votes
1 answer

Making pthread_rwlock_wrlock recursive

I have a problem regarding the behaviour of the pthread function pthread_rwlock_wrlock. The specification linked above states that when one thread has locked the lock for writing and the same thread locks it again, it results in undefined behaviour…
pmf
  • 7,619
  • 4
  • 47
  • 77
7
votes
1 answer

Cursor of HashMap records with RwLockGuard in Rust

I am new to Rust, and I am trying to implement a simple, thread-safe memory key-value store, using a HashMap protected within a RwLock. My code looks like this: use std::sync::{ Arc, RwLock, RwLockReadGuard }; use std::collections::HashMap; use…
Ayman Madkour
  • 325
  • 1
  • 7
6
votes
2 answers

Returning a RWLockReadGuard independently from a method

I have an object of type Arc> And I have a method that is supposed to take some kind of reference to SessionData fn some_method(session: ...) I'm using Rocket (a web-framework for Rust), and I can't directly invoke the method,…
Samuel Moriarty
  • 958
  • 1
  • 8
  • 20
5
votes
1 answer

Implementing write-preferring R/W lock

I have a mutex library, and am trying to implement a write-preferring lock. I am looking at this example: https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock I understand the read-preferring lock, but I don't understand the write-preferring…
user7898461
5
votes
2 answers

Read preferring RW mutex lock in Golang

I need a read preferring RW mutex in golang. Is there a package in golang that will satisfy my needs. I tried sync.RWMutex, but it seems to be write preferring lock. Here goes my attempt to distinguish Go's RWMutex, package main import ( "fmt" …
IhtkaS
  • 1,314
  • 3
  • 15
  • 31
4
votes
1 answer

Why does Rust allow writing to an immutable RwLock?

Let's say we have declared an immutable (not let mut) RwLock instance like: let value = RwLock::new(0); Because the value is immutable, I expected that I can not change the inner value of the RwLock. However when I tested, apparently this works: { …
TheMisir
  • 4,083
  • 1
  • 27
  • 37
4
votes
1 answer

Getting a read-only version of an Arc>?

I have an Arc>. Is there a way to make something out of this, on which the RwLock's write() does not exist? i.e. Is there some form of RLock which I can make from an RwLock. Use case: I have a Foo. More than one part of my code needs…
fadedbee
  • 42,671
  • 44
  • 178
  • 308
3
votes
1 answer

How does `write` mode on a `RwLock` work if it isn't mutable?

So, I was writing some code and apparently R.A. didn't warn me about some erroneous stuff I had written in regards to how ownership works with lambdas. So, a friend helped me rewrite some of my code, and this is just a play example, but their new…
KattyTheEnby
  • 87
  • 1
  • 7
3
votes
0 answers

When is writer-preferred reader-writer lock used?

I know there are many reader-preferred rwlock, and there are even more aggressive design such as RCU, which are usually considered reader-preferred. But what if the writers are not much less than the readers? I know a writer-only rwlock will fall…
Zihan
  • 405
  • 3
  • 13
3
votes
1 answer

Trying to return reference from RwLock, "borrowed value does not live long enough" Error

I've been working on my first Rust project recently but have hit a snag. I am using a HashMap mapping Strings to AtomicUsize integers. The HashMap is protected by a RwLock to allow for concurrent access. I would like to be able to return references…
jeromefroe
  • 1,345
  • 12
  • 19
2
votes
1 answer

Reader Writer Lock Pattern in RUST

I have a scenario like this : Rust Playground I have a struct Container with a lock that is protecting multiple members. I don't want RwLock per member. I want to acquire this lock and then call another function complex_mutation that does some…
Nitish Upreti
  • 6,312
  • 9
  • 50
  • 92
2
votes
1 answer

Rust destructure enum protected by std::sync::RwLock, return with reference

I have a vector of enum elements I'd like to access in a thread-safe manner. I have interfaces that need to degrade the enum values to an option, as to not expose the inner structures to the public API. For that I have enclosed each of the vector…
Dávid Tóth
  • 2,788
  • 1
  • 21
  • 46
2
votes
0 answers

Wrapping RwLock in rust c++ interface?

When trying to design a UI engine in rust and exposing api to c++ logic, my current thinking would be separating the dom and program logic via a binding mechanism. Pseudo code: // in c style, but types are in c++ or rust for clear meaning…
shangjiaxuan
  • 205
  • 1
  • 10
2
votes
0 answers

Array of structs with individual RwLocks for each block

I am trying to create a struct which holds multiple blocks of bytes where each block of bytes is held in an individual RwLock. This is for a voxel engine where performance is very important. Each block of bytes will need to be read/written by…
2
votes
1 answer

Acquiring a RwLock for read and keep it beyond the scope

I have a thread that periodically calls a callback function. Depending on the state, the callback function shall acquire an RwLock of a resource shared with other threads and keep the resource locked even beyond the scope of the callback function.…
1
2 3 4