From OSDev Wiki
Jump to: navigation, search

counting semaphores: ", the process waits until the variable is non-zero, and then atomically decrements it before returning." shouldn't it be "the variable is positive" ? Adek336 01:43, 20 July 2008 (UTC)

Ten years and no-one noticed this code is wrong. I'll update the example when I get an opportunity to work out the specifics. There needs to be another Semaphore around the reading and updating of "last_read". Example: last_read is 7, messages is 3. Two threads can enter get() at the same time, neither block on the wait() as there are pending messages. Thread 1 reads queue[7] and is interrupted. Thread 2 then reads queue[7] and increments last_read to 8 and continues. Thread 1 then increments last_read to 9 and continues. queue[7] was read twice and queue[8] was skipped completely. Kenny 05:19, 3 February 2017 (CST)

Personal tools