Throughout this week, I learned about semaphores, which are
a tool to help threads work safely when they share data. Semaphores use a
counter and two operations: wait (also called down) and post (also called up).
When a thread calls wait, it tries to take a resource, and if the counter is
zero, it has to wait. When a thread calls post, it gives back the resource and
wakes up any waiting thread. A binary semaphore is like a simple lock, where
the counter is either 0 or 1.
One example we saw is the producer-consumer problem, where
one thread puts data into a buffer and another takes it out. If they don’t use
semaphores correctly, they might use the same space at the same time or miss
data. So we use semaphores to manage empty spots, full spots, and to make sure
only one thread changes the buffer at a time.
We also learned about reader-writer locks, where many
threads can read at the same time, but only one can write. This helps when we
want more performance while still avoiding bugs.
Lastly, I also watched a lecture video about the Anderson-Dahlin method. I still need more time to fully understand it, but I know it talks about avoiding deadlocks by carefully scheduling threads based on their potential lock usage. I'll make sure to review this one more time next week.
No comments:
Post a Comment