Mutex: Difference between revisions

Content added Content deleted
No edit summary
Line 573: Line 573:
...
...
</pre>
</pre>

=={{header|M2000 Interpreter}}==
We can simulate mutex
<lang M2000 Interpreter>
Module CheckIt {
Thread.Plan Concurrent
Class mutex {
mylock as boolean=True
Function Lock {
if not .mylock then exit
.mylock<=False
=True
}
Module Unlock {
.mylock<=True
}
}
Group PhoneBooth {
NowUser$
module UseIt (a$, x){
.NowUser$<=a$
Print a$+" phone home ",Int(x*100);"%"
}
module leave {
.NowUser$<=""
}
}
m=mutex()
Flush
Data "Bob", "John","Tom"
For i=1 to 3 {
Thread {
\\ we use N$, C and Max as stack variables for each thread
\\ all other variables are shared for module
If C=0 Then if not m.lock() then Print N$+" waiting...................................": Continue
C++
if c=1 then thread this interval 20
PhoneBooth.UseIt N$,C/Max
iF C<Max Then Continue
PhoneBooth.leave
m.Unlock
Thread This Erase
} as K
Read M$
Thread K Execute Static N$=M$, C=0, Max=RANDOM(20)
Thread K interval Random(300, 2000)
}
\\ Start we lock Phone Booth for service
Service=m.lock()
Main.Task 50 {
\\ a block in a thread run alone
{
If Service Then if Keypress(32) then m.unlock: Service=false: Continue
If not Service then if Keypress(32) Then if m.lock() then Service=true : Continue
if PhoneBooth.NowUser$<>"" Then {
Print "Phone:";PhoneBooth.NowUser$
} Else.if Service then Print "Service Time"
}
}
}
CheckIt

</lang>



=={{header|Nim}}==
=={{header|Nim}}==