Skip to content

Commit d5a3fe3

Browse files
authored
Merge pull request #3635 from rtyley/patch-1
Docs: tweak 'Shared Resource' example
2 parents a99b137 + b1885a5 commit d5a3fe3

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

site/concurrency-primitives.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -213,28 +213,28 @@ When multiple processes try to access a precious resource you might want to cons
213213

214214
Three processes are trying to access a shared resource at the same time but only one at a time will be granted access and the next process have to wait until the resource gets available again (availability is one as indicated by the semaphore counter).
215215

216-
R1, R2 & R3 will request access of the precious resource concurrently so this could be one possible outcome:
216+
P1, P2 & P3 will request access of the precious resource concurrently so this could be one possible outcome:
217217

218218
```
219-
R1 >> Availability: 1
220-
R2 >> Availability: 1
221-
R2 >> Started | Availability: 0
222-
R3 >> Availability: 0
219+
P1 >> Availability: 1
220+
P2 >> Availability: 1
221+
P2 >> Started | Availability: 0
222+
P3 >> Availability: 0
223223
--------------------------------
224-
R1 >> Started | Availability: 0
225-
R2 >> Done | Availability: 0
224+
P1 >> Started | Availability: 0
225+
P2 >> Done | Availability: 0
226226
--------------------------------
227-
R3 >> Started | Availability: 0
228-
R1 >> Done | Availability: 0
227+
P3 >> Started | Availability: 0
228+
P1 >> Done | Availability: 0
229229
--------------------------------
230-
R3 >> Done | Availability: 1
230+
P3 >> Done | Availability: 1
231231
```
232232

233-
This means when R1 and R2 requested the availability it was one and R2 was faster in getting access to the resource so it started processing. R3 was the slowest and saw that there was no availability from the beginning.
233+
This means when P1 and P2 requested the availability it was one and P2 was faster in getting access to the resource so it started processing. P3 was the slowest and saw that there was no availability from the beginning.
234234

235-
Once R2 was done R1 started processing immediately showing no availability.
236-
Once R1 was done R3 started processing immediately showing no availability.
237-
Finally, R3 was done showing an availability of one once again.
235+
Once P2 was done P1 started processing immediately showing no availability.
236+
Once P1 was done P3 started processing immediately showing no availability.
237+
Finally, P3 was done showing an availability of one once again.
238238

239239
```scala mdoc:silent
240240
import cats.effect.{IO, IOApp, Temporal}
@@ -244,9 +244,9 @@ import fs2.Stream
244244

245245
import scala.concurrent.duration._
246246

247-
class PreciousResource[F[_]: Temporal](name: String, s: Semaphore[F]) {
247+
class SlowProcess[F[_]: Temporal](name: String, s: Semaphore[F]) {
248248

249-
def use: Stream[F, Unit] = {
249+
def execute: Stream[F, Unit] = {
250250
for {
251251
_ <- Stream.eval(s.available.map(a => println(s"$name >> Availability: $a")))
252252
_ <- Stream.eval(s.acquire)
@@ -263,10 +263,10 @@ object Resources extends IOApp.Simple {
263263
def run: IO[Unit] = {
264264
val stream = for {
265265
s <- Stream.eval(Semaphore[IO](1))
266-
r1 = new PreciousResource[IO]("R1", s)
267-
r2 = new PreciousResource[IO]("R2", s)
268-
r3 = new PreciousResource[IO]("R3", s)
269-
_ <- Stream(r1.use, r2.use, r3.use).parJoin(3)
266+
p1 = new SlowProcess[IO]("P1", s)
267+
p2 = new SlowProcess[IO]("P2", s)
268+
p3 = new SlowProcess[IO]("P3", s)
269+
_ <- Stream(p1.execute, p2.execute, p3.execute).parJoin(3)
270270
} yield ()
271271
stream.compile.drain
272272
}

0 commit comments

Comments
 (0)