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
1
vote
2 answers

Recursive pthread_rwlock_rdlock on Mac OS X/Darwin

I have following sample code (see code below) that does by thread: A: rd-lock B: wr-lock (waiting) A: rd-lock (recursive) A: rd-unlock (recursive) A: rd-unlock B: wr-locked (wake after wait) B: wr-unlock. Basically read-lock is recursive. It it…
Artyom
  • 31,019
  • 21
  • 127
  • 215
1
vote
1 answer

Create MappedRwLockWriteGuard from a reference in if/else in Rust

I am trying to use parking_lot to write a block of code where I am using the property of a RwLock whenever I don't already have a reference to it: let property = if i_already_have_the_reference { simple_mut_reference_to_property } else { …
Amin Ya
  • 1,515
  • 1
  • 19
  • 30
1
vote
2 answers

Early stop of multiple RwLock::write waiting in Rust

My Rust code uses RwLock to process data in multiple threads. Each thread fills a common storage while using the read lock (e.g. filling up a database, but my case is a bit different). Eventually, the common storage will fill up. I need to pause all…
Yuri Astrakhan
  • 8,808
  • 6
  • 63
  • 97
1
vote
1 answer

How to get a reference to the object inside an RwLock?

This code works: use serde::{Deserialize, Serialize}; use std::sync::{RwLock, Arc}; use ron; #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] struct Foo { first: u8, second: u16, } fn main() { let…
fadedbee
  • 42,671
  • 44
  • 178
  • 308
1
vote
0 answers

Rust: Sending a parking_lot::RwLockReadGuard

Here is the simplified code I am trying to write: use parking_lot::RwLock; let buffers: Box<[RwLock]> = Box::new([ ... ]); let (sender, receiver) = std::sync::mpsc::channel(); thread::spawn(move || { let guard = buffers[ ...…
Coder-256
  • 5,212
  • 2
  • 23
  • 51
1
vote
0 answers

How do I use RWLock to implement many readers-one writers scenario right?

I'm trying to write a method which process some messages. They can be read or write messages. Parallel reads are permitted, but when write lock is aquired all subsequent read lock should wait until write lock is released. So I thught…
Alex Zhukovskiy
  • 9,565
  • 11
  • 75
  • 151
1
vote
1 answer

what happens if copy_to_user is in RW lock?

I implemented copy_to_user() inside read-write lock. My question is what if there is a page fault when executing the copy_to_user, will it get serviced even the lock is not released? If not, how can I achieve this? Any idea would be very…
damingzi
  • 676
  • 10
  • 28
1
vote
2 answers

I'm getting this error: error c2064: term does not evaluate to a function taking 0 arguments, but I don't understand why

I'm having this error: error c2064: term does not evaluate to a function taking 0 arguments. The thing is the function takes 0 arguments and I call 0 arguments, and I don't understand what's wrong. RWLock* rwl = new RWLock(); std::thread…
Alon Pe'er
  • 43
  • 1
  • 6
1
vote
1 answer

pthread rwlock lock/unlock from different threads

The docs says that locking from a thread and unlocking from another a rwlock results in undefined behaviour. I have an array and two threads, one allocating it and one deallocating it, this happens in a cycle, and there are also some threads…
Jelly
  • 4,522
  • 6
  • 26
  • 42
1
vote
1 answer

how to init rwlock_t

I am writing a linux kernel module, and I can't find a way to initialise my read-write lock. I prefer a static init. When I try using RW_LOCK_UNLOCKED, the compiler tells me that it is not defined. rwlock_t lock = RW_LOCK_UNLOCKED;
user1637056
  • 382
  • 3
  • 18
1
vote
0 answers

RWLock using boost shared_mutex, unique_lock and shared_lock

I am tring to implement a RWLock interface interface IRWLock { void acquireLockShared(); //< LockRead. void releaseLockShared(); //< UnLockRead. void acquireLockExclusive(); //< LockWrite. void releaseLockExclusive(); //<…
cprogrammer
  • 5,503
  • 3
  • 36
  • 56
0
votes
1 answer

How do I make an interprocess rwlock in FreeBSD?

What is the way to make an interprocess read-write lock in FreeBSD? I'm looking for something like pthread's rwlock that could be set to be shared between processes by "pthread_rwlockattr_setpshared" and PTHREAD_PROCESS_SHARED flag which…
Nikita Kanashin
  • 125
  • 1
  • 7
0
votes
1 answer

Correct usage of async with tokio in Rust

I never worked with asynchronous code (but have exp with multithreading) in Rust. I thought it is mostly the same thing, however I got some problems. Here listing of my sample: use tokio::{join, sync::RwLock}; use std::sync::Arc; use…
MrZlo
  • 9
  • 3
0
votes
0 answers

Concurrency control design problem with class

I'm having a design problem with designing a class that holds member variables that will be accessed by multiple threads. More precisely, writes to the class member will be done in single thread (mostly during initialization) while reads to the…
YoonSeok OH
  • 647
  • 2
  • 7
  • 15
0
votes
1 answer

Rust "borrowed value does not live long enough" when iterating on RwLock

I have the following code which fails to compile because it says the borrowed value does not live long enough: struct Node { val: RwLock, next: RwLock>>, } impl Node { …
astinov
  • 3
  • 1