-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[question] In project 4, how to use RwLock to improve performance #317
Comments
+1 on this, I feel the instruction is not very accurate. You can't have multiple reader on one open file handler, you need to create new file handlers like in example lock free reader , which means for each thread you create a new set of readers (and underlying file handlers). I am not sure if any real system does this, most has buffer pool manager (which would give you RWLatch on page it has read), some uses mmap (which is unsafe). Creating new readers may not be that bad because the operating system has cache and you are likely to run out of thread before running out of file descriptors. In the end I just skipped most part of project 4, my main take away are internal mutability and how to use channel and threads (didn't learn much on lock free stuff). btw: PingCAP seems to be working a new set of training plans, though I am not sure when it will come out ... |
For now I'm just end up with something like this: struct InnerStore {
folder_path: PathBuf,
index: HashMap<String, u64>,
useless_cmd: usize,
}
pub struct KvStore {
inner: Arc<RwLock<InnerStore>>,
} In each read/write request, I acquire read/write lock to InnerStore, then create new handlers in the method implementation. |
Looks like I'm not the only one got confused, haha. As quoted: Well, IT IS NOT! We need interior mutability for KvStore file, that requires RefCell -- under single thread or protected by Mutex; or RWLock -- shared between threads; but not both. If we are forced to do so, we can only create new RefCell (and reopen KvStore file) again and again under read lock. Took me hours to figure it out, lol. |
Regards to project 4.md
I'm trying to use
RwLock
first, but fell into trouble that I can't just simple swapping the Mutex for RwLock, because in kvsget
method, I can't acquireread
lock to innerSharedKvStore
object, then usereader.read()
relative method to get the result. Becauseseek
andread
method onreader
need to be mutable..So it seems that it's not easy to use
RwLock
for the project? I still can't find a simple way to applyRwLock
for the example struct like this:The text was updated successfully, but these errors were encountered: