This week brought us introduction of semaphores as an alternative or extension of locks to aid in controlling our programs that use multiple threads or resources. As opposed to taking a lock and having conditional variables to signal processes waiting on the lock, semaphores are basically an integer value that is read and altered by two calls, sem_wait() and sem_post().
To start you initialize a semaphore you need to initialize it via sem_init(), typically initialized to the value of 1.
sem_wait() will decrease the integer value by one, and then wait only if the value is not greater than or equal to zero, otherwise it will return
sem_post() is called at the end of the critical section. This will cause the semaphores value to increase by one.
As more threads are waiting on a semaphore it will cause the integer value to decrease (into negative values), and can be used to determine how many threads are waiting.
We also dove into common problems encountered when programming with currency ...bugs. Things like deadlocks, but also issues like atomicity-violation issues . We explored the different kinds of dead locks, like circular wait. We also covered how to avoid those issues.
0 comments:
Post a Comment