Is this code safe?
Posted: Thu Oct 23, 2014 11:20 pm
Good day or night,
I have a situation where I need to add/remove elements to global linked list from multiple threads. For example:
This is working fine. However, theoretically, is it safe to run such kind of code for a long period?(some days). Could this be a reason of mysterious crashes/freezes/IMAs ? Thanks.
I have a situation where I need to add/remove elements to global linked list from multiple threads. For example:
Code: Select all
If Not #PB_Compiler_Thread
MessageRequester("", "Run in threadsafe mode", #MB_OK|#MB_ICONERROR)
End
EndIf
Global NewList Guinea_Pig.s()
Global Mutex = CreateMutex()
Procedure Add_Remove_Element_To_List(Void)
Repeat
Delay(Random(1000))
LockMutex(Mutex)
AddElement(Guinea_Pig())
Guinea_Pig() = Str(Random(9999999))
FirstElement(Guinea_Pig())
DeleteElement(Guinea_Pig(), 1)
UnlockMutex(Mutex)
ForEver
EndProcedure
For i = 1 To 100
CreateThread(@Add_Remove_Element_To_List(), 0)
Next
MessageRequester("", "Click OK to finish")