|
Class: UnlimitedSharedQueue
Object
|
+--Collection
|
+--Queue
|
+--SharedQueue
|
+--UnlimitedSharedQueue
- Package:
- stx:libbasic2
- Category:
- Kernel-Processes
- Version:
- rev:
1.2
date: 2017/01/26 09:40:25
- user: mawalch
- file: UnlimitedSharedQueue.st directory: libbasic2
- module: stx stc-classLibrary: libbasic2
- Author:
- Claus Gittinger
Like the superclass, SharedQueues, this provide a safe mechanism for processes to communicate.
They are basically Queues, with added secure access to the internals,
allowing use from multiple processes (i.e. the access methods use
critical regions to protect against confusion due to a process
switch within a modification).
In contrast to SharedQueues, which block the writer when the queue is full,
instances of me grow the underlying container, so the writer will never block
(of course, the reader will still block in #next, if the queue is empty).
This kind of queue is needed if the reader process itself possibly wants to
add more to the queue. For this, a limited sharedQueue may block the reader,
if this reader process cannot add a new element.
SharedQueue
SharedCollection
OrderedCollection
Queue
Semaphore
Process
CodingExamples::SharedQueueExamples
private
-
commonWriteWith: aBlock
-
common code for nextPut / nextPutFirst;
do NOT wait for available space, if the queue is full; instead resize as required.
After the put, signal availablity of a datum to readers.
ATTENTION:
Using a regular SharedQueue will lead to a deadlock when the reader writes itself.
(you'll have to terminate the two processes in the process monitor):
|reader writer q|
q := SharedQueue new:10.
reader :=
[
[
|element|
element := q next.
element == true ifTrue:[
q nextPut:#xx.
q nextPut:#xx.
q nextPut:#xx.
].
Transcript showCR:element.
] loop.
] fork.
writer :=
[
q nextPut:false.
q nextPut:false.
q nextPut:false.
q nextPut:false.
q nextPut:false.
q nextPut:true.
q nextPut:true.
q nextPut:true.
q nextPut:true.
q nextPut:true.
q nextPut:false.
q nextPut:false.
q nextPut:false.
Transcript showCR:'writer finished'.
] fork.
|
this will not lead to a deadlock
(you'll have to terminate the two processes in the process monitor):
|reader writer q|
q := UnlimitedSharedQueue new:10.
reader :=
[
[
|element|
element := q next.
element == true ifTrue:[
q nextPut:#xx.
q nextPut:#xx.
q nextPut:#xx.
].
Transcript showCR:element.
] loop.
] fork.
writer :=
[
q nextPut:false.
q nextPut:false.
q nextPut:false.
q nextPut:false.
q nextPut:false.
q nextPut:true.
q nextPut:true.
q nextPut:true.
q nextPut:true.
q nextPut:true.
q nextPut:false.
q nextPut:false.
q nextPut:false.
Transcript showCR:'writer finished'.
] fork.
|
|