35 lines
943 B
Plaintext
35 lines
943 B
Plaintext
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
|