I'm wondering, wether each thread has got it's own ressources or not.
This thinking appeares, when I create common procedures and I want to publish them in the PB-forums. The critical point is for example Static variables. Do they have different values although they're called by 2 or more different threads?
I think, they should have different values, but I couldn't watch this behaviour. Let's have a look at this code:
Code: Select all
Procedure expressionError(thread)
CreateRegularExpression(#PB_Any, "(?") ; "unrecognized character after (?"
Delay(4000) ; pretend being busy
Debug RegularExpressionError() ; now we read the error msg (NOT threadsafe: it has been overwritten??)
EndProcedure
thread1=CreateThread(@expressionError(), 0)
Procedure readExpressionError(thread)
CreateRegularExpression(#PB_Any, "*") ; "nothing to repeat"
Delay(0)
Debug RegularExpressionError() ; now we read the error msg
EndProcedure
thread2=CreateThread(@readExpressionError(), 0)
WaitThread(thread1)
WaitThread(thread2)
Both times, when CreateRegularExpression() is called, an error occurs. This error should be read with RegularExpressionError().
Debugoutput NOT threadsafe wrote:nothing to repeat
nothing to repeat
The first one is incorrect, because the error is overwritten by the second thread. Or is it even a bug?Debugoutput threadsafe wrote:nothing to repeat
unrecognized character after (?
The second output is correct. Each thread can read it's own error message.
This leads me to the conclusion that each thread at least with threadsafe mode on has got it's own ressources.
Do you agree? Do I understand this phenomenon right?
Then let's have a look at the second code, regarding the 'critical point', e. g. variables using Static:
Code: Select all
Procedure commonUsed()
Static expression
If Not expression
expression=CreateRegularExpression(#PB_Any, ".*")
Debug "regExp has ben created"
EndIf
EndProcedure
Procedure expressionError(thread)
commonUsed()
Delay(4000)
EndProcedure
thread1=CreateThread(@expressionError(), 0)
Procedure readExpressionError(thread)
commonUsed()
Delay(4000)
EndProcedure
thread2=CreateThread(@readExpressionError(), 0)
WaitThread(thread1)
WaitThread(thread2)
What does the debugger say? It indicates that the 'object' is only being created once:
Code: Select all
regExp has ben created
Final Conclusion:
Well, is the conclusion from the first code incorrect? Do threads only have different ressources at PureBasic's wil or randomly? What's the general rule?
This leads me to the point where I avoid coding procedures with static variables that should be able to work correctly in ANY case: normal mode, threadsafe, unicode+threadsafe, ...
This behaviour can be a problem: Imagine each thread sends and receives data via network and calls a commonly used procedure. If this procedure then uses a static buffer, everything ends in a mess..
So let's have a nice discussion about this and I hope you can explain it to me. Morover, this thread is hopefully useful for others, too.