You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dyon uses "in-types" to communicate across threads. A common pattern when receiving data is the following:
fnreport(a: in[[f64]]){loop{
x := next(a)if x == none(){break}
x := unwrap(x)
print(x[0])}println("")}
With the for-in loop, one can write:
fnreport(a: in[[f64]]){for x in a {print(x[0]}println("")}
The syntax is similar to the iterator pattern in Rust. Dyon does not have iterators at this moment, but the semantics is very similar. The loop is sequentially executed like in Rust, but the data generated might come from several threads at once.
In the future, Dyon might get iterator/generator syntax, which probably will use the same in-type.
The for-in loop breaks when there are no new messages. A common pattern is to create an extra in-type to signal when a task is complete and use a counter to detect when all tasks are complete:
fnlog(a:f64){}fnfinish(){}fnbar(id:f64,n:f64) -> bool{for i n {
log((id + 1)*1000 + i)
sleep(0.1)}
finish()
return true}fn main(){
log := in log
finish := in finish
n := 10fori n {_:= go bar(i,2)}
loop {for msg in log {println(msg[0])}if n == 0{break}for done in finish {n -= 1}}}
Notice that the check if n == 0 {break} happens after emptying the queue for messages, but before decrementing the counter. This makes sure that there is no data race.
Design:
Use Rust's syntax for iterators to read messages from in-types in Dyon
Extensible for other kinds of patterns in the future such as iterators/generators
Rules:
Non-blocking (similar to next).
Must be wrapped in a loop with proper checks to avoid data races.
The text was updated successfully, but these errors were encountered:
Dyon uses "in-types" to communicate across threads. A common pattern when receiving data is the following:
With the for-in loop, one can write:
The syntax is similar to the iterator pattern in Rust. Dyon does not have iterators at this moment, but the semantics is very similar. The loop is sequentially executed like in Rust, but the data generated might come from several threads at once.
In the future, Dyon might get iterator/generator syntax, which probably will use the same in-type.
The for-in loop breaks when there are no new messages. A common pattern is to create an extra in-type to signal when a task is complete and use a counter to detect when all tasks are complete:
Notice that the check
if n == 0 {break}
happens after emptying the queue for messages, but before decrementing the counter. This makes sure that there is no data race.Design:
Rules:
next
).The text was updated successfully, but these errors were encountered: