Skip to content

Commit 3070bc3

Browse files
ported Event example from old SimJulia doc (#107)
Co-authored-by: Stefan Krastanov <[email protected]> Co-authored-by: Stefan Krastanov <[email protected]>
1 parent fdd4eff commit 3070bc3

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# News
22

3+
## v1.4.1 - dev
4+
5+
- Added examples to the documentation that were lost around the time of the rewrite for v0.5 in 2018.
6+
37
## v1.4.0 - 2023-08-07
48

59
- Implement a `DelayQueue`, i.e. a `QueueStore` with latency between the store and take events.

docs/src/guides/events.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,71 @@ ConcurrentSim.Event 1
5858
5959
julia> run(sim)
6060
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))")
103+
end
104+
end
105+
106+
env = Simulation()
107+
school = School(env)
108+
run(env)
109+
110+
# output
111+
112+
pupil 1 goes to class
113+
pupil 2 goes to class
114+
pupil 3 goes to class
115+
starting the bell timer at t=0.0
116+
bell is ringing at t=45.0
117+
starting the bell timer at t=45.0
118+
pupil 1 leaves class at t=45.0
119+
pupil 1 goes to class
120+
pupil 2 leaves class at t=45.0
121+
pupil 2 goes to class
122+
pupil 3 leaves class at t=45.0
123+
pupil 3 goes to class
124+
bell is ringing at t=90.0
125+
pupil 1 leaves class at t=90.0
126+
pupil 2 leaves class at t=90.0
127+
pupil 3 leaves class at t=90.0
128+
```

src/resources/containers.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function Container{T}(env::Environment, capacity::N=one(N); level=zero(N)) where
4040
Container{N, T}(env, capacity; level=N(level))
4141
end
4242

43+
"""An alias for `Container{Int, Int}`, one of the most frequently used types of synchronization primitive."""
4344
const Resource = Container{Int, Int}
4445

4546
function put!(con::Container{N, T}, amount::N; priority=zero(T)) where {N<:Real, T<:Number}

0 commit comments

Comments
 (0)