#DOCUMENTATION by cg
class: SharedQueue
category of:
#nextPut:
#nextPutFirst:
class: SharedQueue class
comment/format in:
#documentation
#examples
--- a/SharedQueue.st Tue Sep 25 18:28:02 2018 +0200
+++ b/SharedQueue.st Wed Sep 26 15:21:09 2018 +0200
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
"
COPYRIGHT (c) 1993 by Claus Gittinger
All Rights Reserved
@@ -50,11 +52,11 @@
For nonBlocking read, use #isEmpty; for nonBlocking write, use #isFull.
Be warned:
- if the reader process wants to add elements to the sharedqueue in its
- read-loop, the reader may block, if the queue is full.
- The reason is that the sharedQueues size is fixed, and any writer is blocked
+ if a reader process wants to add elements to the sharedqueue in its
+ read-loop, it may block, if the queue is full, leading to a deadlock.
+ The reason is that the sharedQueues size is fixed, and any write is blocked
if the queue is full.
- For this situations, please use an UnlimitedSharedQueue, which grows in this
+ For this situations, please use an UnlimitedSharedQueue, which grows in that
particular situation.
See samples in doc/coding.
@@ -74,6 +76,7 @@
examples
"
+ [exBegin]
|queues readers writers seqNumber accessLock accessLock2
numbersStillToReceive|
@@ -118,6 +121,42 @@
(numbersStillToReceive includes:true) ifTrue:[
self halt:'oops - not all numbers received'
]
+ [exEnd]
+
+ deadlock example:
+ here, a read process tries to write !!
+ [exBegin]
+ |queue reader writer|
+
+ queue := SharedQueue new:10.
+ reader :=
+ [
+ |num|
+
+ [ (num := queue next) ~~ #EOF] whileTrue:[
+ 'here is the bad code: writing into the queue !!'.
+ num == 30 ifTrue:[
+ Transcript showCR:'xxx'.
+ queue nextPut:'bad1'.
+ queue nextPut:'bad2'.
+ ].
+ Transcript showCR:num.
+ Delay waitForSeconds:0.01.
+ ].
+ ] fork.
+
+ writer :=
+ [ |num|
+
+ 1 to:60 do:[:seqNr |
+ queue nextPut:seqNr.
+ ].
+ queue nextPut:#EOF.
+ ] fork.
+
+ reader waitUntilTerminated.
+ writer waitUntilTerminated.
+ [exEnd]
"
! !
@@ -328,7 +367,7 @@
^ super peek
! !
-!SharedQueue methodsFor:'adding'!
+!SharedQueue methodsFor:'accessing-writing'!
nextPut:anObject
"enter anObject to the end of the queue;