Page 1 of 1

A question on threads.

Posted: Mon Jul 17, 2006 3:19 pm
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.

Posted: Mon Jul 17, 2006 4:41 pm
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.

Posted: Mon Jul 17, 2006 6:40 pm
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.

Posted: Mon Jul 17, 2006 9:23 pm
by srod
Thanks for the replies.

I thought that would be the case, but worth knowing for definite. :)