Skip to content

[Question] 为什么第八章 并发中的condvar.wakup不直接带上mutex? #94

Open
@nkbai

Description

@nkbai

https://learningos.github.io/rust-based-os-comp2022/chapter8/4condition-variable.html 中,
conditional variable 的wakeup函数是不带任何参数的,例如:

unsafe fn first() -> ! {
     mutex.lock();
     A=1;
     condvar.wakup();
     mutex.unlock();
 
 }

为什么不直接设计成下面的样子呢?

unsafe fn first() -> ! {
     mutex.lock();
     A=1;
/*
     condvar.wakup();
     mutex.unlock();
  这两句话,替换成下面的一句
*/
     condvar.wakup(mutex);
 
 }

好处是,避免了second被唤醒以后,发现获取不到锁而再次休眠。

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions