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
Copy file name to clipboardExpand all lines: docs/src/guides/events.md
+68-1Lines changed: 68 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -58,4 +58,71 @@ ConcurrentSim.Event 1
58
58
59
59
julia> run(sim)
60
60
Called back from ConcurrentSim.Event 1
61
-
```
61
+
```
62
+
63
+
## Example usages of Event
64
+
65
+
The simple mechanics outlined above provide a great flexibility in the way events can be used.
66
+
67
+
One example for this is that events can be shared. They can be created by a process or outside of the context of a process. They can be passed to other processes and chained.
68
+
69
+
Below we give such an example, however this is a **very low-level example** and you would probably prefer to use the safer and more user-friendly [`Resource`](@ref) or [`Store`](@ref).
70
+
71
+
```jldoctest
72
+
using ResumableFunctions
73
+
using ConcurrentSim
74
+
75
+
mutable struct School
76
+
class_ends :: Event
77
+
pupil_procs :: Vector{Process}
78
+
bell_proc :: Process
79
+
function School(env::Simulation)
80
+
school = new()
81
+
school.class_ends = Event(env)
82
+
school.pupil_procs = Process[@process pupil(env, school, i) for i=1:3]
83
+
school.bell_proc = @process bell(env, school)
84
+
return school
85
+
end
86
+
end
87
+
88
+
@resumable function bell(env::Simulation, school::School)
89
+
for i=1:2
90
+
println("starting the bell timer at t=$(now(env))")
91
+
@yield timeout(env, 45.0)
92
+
succeed(school.class_ends)
93
+
school.class_ends = Event(env) # the event is now idle (i.e. spent) so we need to create a new one
94
+
println("bell is ringing at t=$(now(env))")
95
+
end
96
+
end
97
+
98
+
@resumable function pupil(env::Simulation, school::School, pupil)
99
+
for i=1:2
100
+
println("pupil $pupil goes to class")
101
+
@yield school.class_ends
102
+
println("pupil $pupil leaves class at t=$(now(env))")
0 commit comments