Metered concurrency: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl 6}}: not limited to JVM)
Line 1,057: Line 1,057:
yes
yes
</lang>
</lang>

=={{header|Oforth}}==

A semaphore can be emulated with a channel starting with n objects.
Acquiring the semaphore is receiving an object from the channel
Releasing the semaphore is sending by the object into the channel.

If the channel is empty a task will wait until it is no more empty.

<lang oforth>
import: parallel

Object Class new: Semaphore(ch)

Semaphore method: initialize(n)
Channel newSize(n) dup := ch
#[ 1 over send drop ] times(n) drop ;

Semaphore method: acquire @ch receive drop ;
Semaphore method: release 1 @ch send drop ;</lang>

Usage :

<lang oforth>: mytask(s)
while( true ) [
s acquire "Semaphore acquired" .cr
2000 sleep
s release "Semaphore released" .cr
] ;


: test(n)
| s i |
Semaphore new(n) ->s
10 loop: i [ #[ s mytask ] & ] ;</lang>


=={{header|Oz}}==
=={{header|Oz}}==