Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
34
Task/Mutex/PureBasic/mutex-2.basic
Normal file
34
Task/Mutex/PureBasic/mutex-2.basic
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
Declare ThreadedTask(*MyArgument)
|
||||
Define Mutex
|
||||
|
||||
If OpenConsole()
|
||||
Define thread1, thread2, thread3
|
||||
|
||||
Mutex = CreateMutex()
|
||||
thread1 = CreateThread(@ThreadedTask(), 1): Delay(5)
|
||||
thread2 = CreateThread(@ThreadedTask(), 2): Delay(5)
|
||||
thread3 = CreateThread(@ThreadedTask(), 3)
|
||||
WaitThread(thread1)
|
||||
WaitThread(thread2)
|
||||
WaitThread(thread3)
|
||||
|
||||
PrintN(#CRLF$+"Press ENTER to exit"): Input()
|
||||
FreeMutex(Mutex)
|
||||
CloseConsole()
|
||||
EndIf
|
||||
|
||||
Procedure ThreadedTask(*MyArgument)
|
||||
Shared Mutex
|
||||
Protected a, b
|
||||
For a = 1 To 3
|
||||
LockMutex(Mutex)
|
||||
; Without Lock-/UnLockMutex() here the output from the parallel threads would be all mixed.
|
||||
; Reading/Writing to shared memory resources are a common use for Mutextes i PureBasic
|
||||
PrintN("Thread "+Str(*MyArgument)+": Print 3 numbers in a row:")
|
||||
For b = 1 To 3
|
||||
Delay(75)
|
||||
PrintN("Thread "+Str(*MyArgument)+" : "+Str(b))
|
||||
Next
|
||||
UnlockMutex(Mutex)
|
||||
Next
|
||||
EndProcedure
|
||||
Loading…
Add table
Add a link
Reference in a new issue