Singleton: Difference between revisions

Content added Content deleted
(→‎{{header|Go}}: expanded with sync.Once examples)
Line 1,413: Line 1,413:
Oforth does not have global variables, class attributes or some kind of shared mutable memory that can be updated by different tasks.
Oforth does not have global variables, class attributes or some kind of shared mutable memory that can be updated by different tasks.


In Oforth, singleton is an anti-pattern because it needs synchronisation in order to be safe between tasks.
In Oforth, singleton is an anti-pattern because it needs synchronisation in order to be safe between parallel tasks.


If the goal is to keep and update a value in a safe way, a channel can be used.
If the goal is to keep and update a value in a safe way, a channel can be used.
Line 1,421: Line 1,421:
<lang Oforth>Object Class new: Sequence(channel)
<lang Oforth>Object Class new: Sequence(channel)
Sequence method: initialize(initialValue)
Sequence method: initialize(initialValue)
{
Channel newSize(1) := channel
Channel newSize(1) := channel
@channel send(initialValue) drop
@channel send(initialValue) drop ;
}


Sequence method: nextValue { @channel receive dup 1 + @channel send drop }</lang>
Sequence method: nextValue @channel receive dup 1 + @channel send drop ;</lang>


Usage :
Usage :
<lang Oforth>func: testSequence
<lang Oforth>import: parallel

{
: testSequence
| s i |
| s i |
Sequence new(0) ->s
Sequence new(0) ->s
100 loop: i [ #[ s nextValue println ] & ]
100 loop: i [ #[ s nextValue println ] & ] ;</lang>
}</lang>


=={{header|ooRexx}}==
=={{header|ooRexx}}==