Code Monkey home page Code Monkey logo

Comments (3)

kingdoctor123 avatar kingdoctor123 commented on August 28, 2024

In fact, happens-before is much stricter than simply "spawned before". For more information, you can read https://en.wikipedia.org/wiki/Happened-before. To put it simply, you can understand it as a causal order between operations.

when(c1, c2) { // b1
    when(c2, c3) { ... } // b2
}

when(c3, c4) { // b3
}

In your example,

  • b1 happens-before b3, since they are scheduled sequentially on a same thread. Due to this causality, it is never the case that b3 is executed earlier than b1.
  • b1 happens-before b2, since b2 can be scheduled only after b1 is scheduled and executed. Due to this causality, it is never the case that b2 is executed earlier than b1.
  • But, we cannot say either b2 happens-before b3 or b3 happens-before b2, because there are no causal ordering between them. Since there is no causality, b2 can be executed earlier than b1, and b1 can be executed earlier than b1.

However, in the following case, you have b3 happens-before b2.

let x = false; // x: AtomicBool
when(c1, c2) { // b1
    if x.load(SeqCst) {
        when(c2, c3) { ... } // b2
    }
}

when(c3, c4) { // b3
}
x.store(true, SeqCst);

In this code, b3 happens-before b2 holds, since b2 is scheduled only if x.store(true, SeqCst) is executed before x.load(SeqCst). This gives a causal order between b2 and b3.

from cs431.

wyhallenwu avatar wyhallenwu commented on August 28, 2024

Thank you for your explanation, just for double check if I understand it. In the hw boc.rs test case boc::boc.

    when!(c1, c2; g1, g2; { \\b1
        *g1 += 1;
        *g2 += 1;
        when!(c3, c2; g3, g2; { \\b2
            *g2 += 1;
            *g3 = true;
        });
    });

    when!(c1, c2_, c3_; g1, g2, g3; { \\b3
        assert_eq!(*g1, 1);
        assert_eq!(*g2, if *g3 { 2 } else { 1 });
        finish_sender.send(()).unwrap();
    });

In this case, the b1 happens before b2 and b3. b2 can be only scheduled after b1 scheduled and excuted. but there is no certain order between b2 and b3 cause they don't have overlapping cowns.

from cs431.

kingdoctor123 avatar kingdoctor123 commented on August 28, 2024

b2 and b3 do have overlapping cowns (note that c2_ is a cloned version of c2 and c3_ is a cloned version of c3). However, since they do not have causal order, b2 and b3 can be executed in any order.

from cs431.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.