Jump to content
  • Advertisement
Sign in to follow this  
Esmo2000

Threading question

This topic is 4496 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Suppose the following: bool test = true; while(test == true) { do_code(); } The compiler obviously catches an optimisation here as it assumes it is single threaded and simply makes the while loop ignore the test and loop forever. If my application were multi-threaded, I might change bool to be of the type volatile bool, and then perhaps it might be changed by another thread and I would have to test every time. However, what might the compiler do for something like this? bool *test; *test = true; while(*test == true) { do_code(); } If you de-reference a variable, does it check every time? I.e. should I make this volatile bool *test ?? Cheers, J

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
That code will probably just crash.

Share this post


Link to post
Share on other sites
Quote:
Original post by Esmo2000
The compiler obviously catches an optimisation here as it assumes it is single threaded and simply makes the while loop ignore the test and loop forever.

I'm actually not so sure the compiler will make that assumption, and do that. Have you tested this? What compiler are we talking about?

Quote:
Original post by Anonymous Poster
That code will probably just crash.

Probably, yes. You only declare the pointer. I think you mean:
bool test;
test = true;
bool *testPtr = &test;
while( *testPtr == true ) {
do_code();
}
EDIT: Oh, too late. A new declaration, also an option [smile]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I think, really, what he's getting at is:

volatile bool b = true;
bool* test = &b;
...

But the compiler sees a 'volatile bool*' and 'bool*' as different types, so it'll raise an error anyway.

Share this post


Link to post
Share on other sites
Quote:
Original post by Esmo2000
The compiler obviously catches an optimisation here as it assumes it is single threaded and simply makes the while loop ignore the test and loop forever.
If the variable is in local scope like that, then yes, maybe. If it is in global scope and is written to by other functions then it won't, irrespective of if your code is multithreaded or not.

Edit: Ok, I think I misunderstood you. If you need to tell the compiler that a variable may change at any time, then you should use volatile. This kind of information sharing between threads is usually quite brittle and error prone, however.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!