Using read write locks (example in C)

Sdílet
Vložit
  • čas přidán 21. 08. 2024
  • Patreon ➤ / jacobsorber
    Courses ➤ jacobsorber.th...
    Website ➤ www.jacobsorbe...
    ---
    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.jacobsorbe...
    people.cs.clem...
    persist.cs.clem...
    To Support the Channel:
    + like, subscribe, spread the word
    + contribute via Patreon --- [ / jacobsorber ]
    Source code is also available to Patreon supporters. --- [jsorber-youtub...]

Komentáře • 20

  • @pierreabbat6157
    @pierreabbat6157 Před měsícem +4

    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 Před měsícem +4

    This is an awesome video. Fantastic presentation of the material!

  • @weirddan455
    @weirddan455 Před měsícem +3

    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 Před měsícem

    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 Před měsícem +1

    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 Před měsícem

    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.

  • @DanielPaunescu
    @DanielPaunescu Před měsícem +3

    Great video! Could you make one where you cover synchronization based on compare and swap intrinsic?

    • @JacobSorber
      @JacobSorber  Před měsícem +2

      yeah, that's probably doable. I'll add it to the list. thanks

  • @rogo7330
    @rogo7330 Před měsícem +1

    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.

  • @redcrafterlppa303
    @redcrafterlppa303 Před měsícem

    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.

  • @anon_y_mousse
    @anon_y_mousse Před měsícem

    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 Před měsícem

    how to understand c code from decompilers like binary ninja ?

    • @mariomaliqi184
      @mariomaliqi184 Před měsícem +2

      ANALYZE EVERY SINGLE BIT

    • @dookshi
      @dookshi Před měsícem +3

      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 Před měsícem

      What your are asking is how to understand assambly code.
      Well… you understand it by studying about it 🤷

  • @illegalsmirf
    @illegalsmirf Před měsícem

    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 Před měsícem

    HAHA in Russia no adds in youtube :)