A question on threads.

Everything else that doesn't fall into one of the other PB categories.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

A question on threads.

Post by srod »

I was just contemplating threads and what it means exactly to be threadsafe?

One question to the enlightened masses! If two threads, for example, both call the same procedure within the main application (I assume this is possible?) and assuming they each get their own local variables, what happens in the case of static variables?

Specifically, suppose thread A calls procedure Test() and sets static variable 'StaticByte' to equal 10.
Thread B now calls Test(). Will 'StaticByte' hold the value of 10?

Sorry if it's a dumb question, but I have little experience with threads.

I guess I could try it out, but I'm away from my Purebasic machine at the moment.

Thanks.
I may look like a mule, but I'm not a complete ass.
KarLKoX
Enthusiast
Enthusiast
Posts: 681
Joined: Mon Oct 06, 2003 7:13 pm
Location: France
Contact:

Post by KarLKoX »

There is no problem with accessing to a variable from 2 or more different thread but the situation is different when you want to modify them, this is why a mutual exclusion mechanism is used and we are in a threadsafe context.
"Qui baise trop bouffe un poil." P. Desproges

http://karlkox.blogspot.com/
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Yes, you can modify the same static variable in a procedure from multiple threads and its new value is recognized by all the threads that access it. This holds true whether "Threadsafe exe" is checked or not.
BERESHEIT
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Thanks for the replies.

I thought that would be the case, but worth knowing for definite. :)
I may look like a mule, but I'm not a complete ass.
Post Reply