Mutex: Difference between revisions

m
Line 291:
=={{header|PureBasic}}==
 
'''PureBasic has the following Mutex functions;'''
<lang PureBasic>MyMutex=CreateMutex()
Result = TryLockMutex(MyMutex)
Line 309:
thread2 = CreateThread(@ThreadedTask(), 2): Delay(5)
thread3 = CreateThread(@ThreadedTask(), 3)
WaitThread(thread1)
WaitThread(thread2)
Line 321 ⟶ 320:
Procedure ThreadedTask(*MyArgument)
Shared Mutex
DefineProtected 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
Line 329 ⟶ 330:
PrintN("Thread "+Str(*MyArgument)+" : "+Str(b))
Next
UnlockMutex(Mutex)
Next
Anonymous user