Using read write locks (example in C)

  Рет қаралды 4,371

Jacob Sorber

Jacob Sorber

Ай бұрын

Patreon ➤ / jacobsorber
Courses ➤ jacobsorber.thinkific.com
Website ➤ www.jacobsorber.com
---
Using read write locks (example in C) // Mutex locks are our go-to lock when we need to protect data structures shared between multiple threads, but sometimes we need more. This video talks about the scenario where you have reader threads and writer threads and the reader/writer locks that can be very helpful (and more performant).
Related Videos:
Threads basics: • How to create and join...
Thread safety and locks: • Safety and Speed Issue...
***
Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.
About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.
More about me and what I do:
www.jacobsorber.com
people.cs.clemson.edu/~jsorber/
persist.cs.clemson.edu/
To Support the Channel:
+ like, subscribe, spread the word
+ contribute via Patreon --- [ / jacobsorber ]
Source code is also available to Patreon supporters. --- [jsorber-youtube-source.heroku...]

Пікірлер: 20
@pierreabbat6157
@pierreabbat6157 26 күн бұрын
The correct term is readers-writer lock (there can be many readers at once, but only one writer at once). It's called shared_mutex in C++.
@lasshrugged
@lasshrugged 28 күн бұрын
This is an awesome video. Fantastic presentation of the material!
@weirddan455
@weirddan455 28 күн бұрын
I never knew this type of lock existed in pthread. I've used mutex quite a bit but my use-cases have been mostly single reader and single writer where mutex does basically the same job. This seems very useful if you have multiple readers though.
@scottduckworth3299
@scottduckworth3299 17 күн бұрын
As mentioned at the end, reader locks do incur a performance overhead over mutex locks. Even if your critical section is only reading the locked variables, you may be better off just using a mutex lock. Usually you need to have a lot of readers with rather long critical sections for rwlocks to perform better than mutex locks.
@mensaswede4028
@mensaswede4028 25 күн бұрын
There is also the issue of holding a reader lock and wanting to upgrade to a writer lock without releasing the reader lock (thereby allowing another writer to sneak in), which allows you to test-and-set all while holding the lock. It’s tricky though, because it’s prone to deadlock.
@scottduckworth3299
@scottduckworth3299 17 күн бұрын
Sometimes it's useful to think of these as exclusive/shared locks. Normally you would write under an exclusive lock but read under a shared lock, but that's not always the case. For example, if you already have a thread safe object, you may want to write to it under a shared lock, but to read a consistent view of the object you would use an exclusive lock.
@redcrafterlppa303
@redcrafterlppa303 26 күн бұрын
It's interesting and kind of sketchy at the same time to cast a long to a void* and back. I would have allocated a count variable on the heap and passed that.
@rogo7330
@rogo7330 27 күн бұрын
They also called exclusive lock for write and shared lock for read. Readers block creation of writer lock, but do not block creation of other reader locks. Writer lock blocks creation of all new locks. This thing also exist for files on file system, but their description and behavior is kinda crazy unfortunatly, so no easy to follow standards there.
@DanielPaunescu
@DanielPaunescu 28 күн бұрын
Great video! Could you make one where you cover synchronization based on compare and swap intrinsic?
@JacobSorber
@JacobSorber 27 күн бұрын
yeah, that's probably doable. I'll add it to the list. thanks
@anon_y_mousse
@anon_y_mousse 26 күн бұрын
I'm kind of curious why you're not also showing the standard way to handle threads. I know that you know they added threading in C11, so why not do a video showing how to handle the same thing using the standard library? The interesting thing is, on the Linux system I'm using, the standard functions and types are just aliases for pthreads and I have to tell the linker to link the pthreads library to get standard threads to work. I'm sure MS and Apple both use their own facilities for threading, but I don't use either so you'd have to look for me.
@tkan2653
@tkan2653 28 күн бұрын
how to understand c code from decompilers like binary ninja ?
@mariomaliqi184
@mariomaliqi184 28 күн бұрын
ANALYZE EVERY SINGLE BIT
@dookshi
@dookshi 28 күн бұрын
That casting in 2:50 and 4:30 hurts my ocd. :-) You could have at least used uintptr_t instead of long. Casting long to void* and back again isn't very portable or is it?
@greyshopleskin2315
@greyshopleskin2315 28 күн бұрын
What your are asking is how to understand assambly code. Well… you understand it by studying about it 🤷
@illegalsmirf
@illegalsmirf 26 күн бұрын
y r u not usin Rust? u is usin C in 2024? dat's tranzfobic r u a Trump voter too?
@user-vw1fg9hr8m
@user-vw1fg9hr8m 28 күн бұрын
HAHA in Russia no adds in youtube :)
How do I access a single bit?
11:07
Jacob Sorber
Рет қаралды 20 М.
Why Is He Unhappy…?
00:26
Alan Chikin Chow
Рет қаралды 71 МЛН
Finger Heart - Fancy Refill (Inside Out Animation)
00:30
FASH
Рет қаралды 29 МЛН
Fast and Furious: New Zealand 🚗
00:29
How Ridiculous
Рет қаралды 45 МЛН
The What, How, and Why of Void Pointers in C and C++?
13:12
Jacob Sorber
Рет қаралды 52 М.
WHY did this C++ code FAIL?
38:10
The Cherno
Рет қаралды 242 М.
Pulling Back the Curtain on the Heap
21:38
Jacob Sorber
Рет қаралды 36 М.
It’s time to move on from Agile Software Development (It's not working)
11:07
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 159 М.
Writing My Own Database From Scratch
42:00
Tony Saro
Рет қаралды 197 М.
How different are C and C++? Can I still say C/C++?
10:25
Jacob Sorber
Рет қаралды 220 М.
Making variables atomic in C
11:12
Jacob Sorber
Рет қаралды 36 М.
Andrew Kelley   Practical Data Oriented Design (DoD)
46:40
ChimiChanga
Рет қаралды 65 М.