On Mon, Jun 5, 2023 at 5:08 AM fliter <
imc...@gmail.com> wrote:
>
> Thanks for your answer. But I wonder if the elements behind the queue may wait for a very long time? Can we maintain a waiting time after entering the queue for each coroutine, and acquire locks from high to low
The queue is already FIFO ordered. The goroutine at the head of the
queue is the one that has been in the queue the longest. I don't see
> 在2023年6月4日星期日 UTC+8 01:07:52<Ian Lance Taylor> 写道:
>>
>> On Sat, Jun 3, 2023 at 12:35 AM fliter <
imc...@gmail.com> wrote:
>> >
>> >
>> > In sync/mutex.go, there is a comment like the following:
>> >
>> > ```go
>> > // Mutex fairness.
>> > //
>> > // Mutex can be in 2 modes of operations: normal and starvation.
>> > // In normal mode waiters are queued in FIFO order, but a woken up waiter
>> > // does not own the mutex and competes with new arriving goroutines over
>> > // the ownership. New arriving goroutines have an advantage -- they are
>> > // already running on CPU and there can be lots of them, so a woken up
>> > // waiter has good chances of losing. In such case it is queued at front
>> > // of the wait queue. If a waiter fails to acquire the mutex for more than 1ms,
>> > // it switches mutex to the starvation mode.
>> > ```
>> >
>> >
>> > I wonder if the waiter here refers to the goroutine at the head of the queue, or any goroutine in the queue?
>>
>> In that comment "waiter" refers to the goroutine at the head of the
>> queue. When a mutex is unlocked, it wakes up the first waiter, if
>> any.
>>
>> Ian
>