Skip to main content
gamedev.net
🔒 Locked

The Volatile Modifier

Started by Evil Bill Jul 22, 2002 at 4:25 PM 4 replies 250+ views
Original Post
Evil Bill
Evil Bill
I''m writing a small chat program with WinSock, and i was wondering if people ever use the volatile modifier. My code goes like this:
  
DWORD Socket::ListenThreadProc()
{
   s_bListenThreadActive = TRUE;
   while(s_bListenThreadActive)
   {
   }
   return 0;
}
  
where "s_bListenThreadActive" is a volatile static BOOL. When the socket is closed, s_bListenThreadActive is set to FALSE, so the thread proc exits. As i said, do people ever use the volatile keyword? Cheers, Steve Steve
DirectX Programmer
Soon to be the new Bill Gates
Member of the Unban Mindwipe Society (UMWS)
niyaw
niyaw
i always do.

---
Come to #directxdev IRC channel on AfterNET
Anon Mike
Anon Mike
If you don''t your app will probably fail when you build in release mode because the while loop will be optimized away and you''ll go into an infinite loop.
-Mike
Evil Bill
Evil Bill
Yeah, thats what i was thinking.
I only ask because i only discovered this a wee while ago, and it hasn''t been mentioned in any multi-threading tutorials i''ve seen - probably because this is a really bad way to do it
I''ve changed my code anyway, so i don''t use threads.

Thanks for the replies, Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates
Member of the Unban Mindwipe Society (UMWS)
Shannon Barber
Shannon Barber
That''s not a bad way to do it, it''s simple & it works - so long as that bool is volatile

You would need a sleep in the loop & it would work good enough.

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows| Spirit(xBNF)]
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
BaelWrath
BaelWrath
Volatile bools arent so bad but if you are ever using something more complicated (say to hold an interupt value) I suggest you copy the value to another variable first then test on the none volatile variable.

This is because the system reads a volatile variable every time it finds it, even if you are using it twice on the same line (say in a case to select a number of subprocedures with the value as a paramter). In other words it can change after the test.

They also make a mess of any optimisation and because of the ''always read'' premise can increase the number of cycles a loop uses quite dramatically.

Regards

BaelWrath

If it is not nailed down it''s mine and if I can prise it loose,
it''s not nailed down!
BaelWrathIf it is not nailed down it's mine and if I can prise it loose,it's not nailed down!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.