Thread: [Dev-C++] C keyword 'volatile'
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Louis G. <lg...@ya...> - 2003-06-19 13:14:13
|
Can anyone explain the C keyword 'volatile' as in the statement: volatile int x; What does this do to variable x ? when does one need to use it ? __________________________________ Do you Yahoo!? SBC Yahoo! DSL - Now only $29.95 per month! http://sbc.yahoo.com |
|
From: Zodiaq <zo...@ac...> - 2003-06-19 13:41:38
|
Hello Louis and all Dev-C++ users, Thursday, June 19, 2003, 3:14:13 PM, Louis wrote as follows: LG> Can anyone explain the C keyword 'volatile' LG> as in the statement: volatile int x; LG> What does this do to variable x ? when does one need to use it ? volatile is very interesting keyword - it does nothing to variable but does something IMPORTANT to compiler optimization concering this variable... and what it does? It turns OFF optimizations like checking value of variable... normally variable value might be checked once in a loop (if it does not change during the loop) at first use but with volatile you force compiler to ALWAYS check it even if use it inside a loop... where is this keyword mainly used?? Hmmm... from my point of view its use is mainly useful in multithreaded application to check statesments that other threads might set - in few of my apps main thread launches child threads to do some work... this work is sometime very time consuming, so if user decides to stop this thread main thread sets one variable of child thread (lets say named Stopped) to true so its knows to stop working... before it returns it makes memory cleanup - frees memory used by thread... If I would just kill it (using TerminateThread) I'd get memory leaks - memory that child thread allocated isn't freed if it's stopped by TerminateThread... so child just checks this Stopped variable in few places (in loops of course) and if its value changes to true it knows to free memory and return... without volatile on Stopped thanks to compiler optimisations this child thread might never know that this value changed - child thread did not change it so why to check its value so often??? volatile does the job! I don't know about any other usages of volatile, but I'd gladly hear about some... -- regards, Zodiaq |
|
From: <a_g...@ya...> - 2003-06-19 14:10:08
|
I think that is the most important use. Really good answer. It is also used when working with shared memory between UNIX or UNIX like OSs. Another interesting use of this variable is in the OS VxWorks, which has a flat gobal memory architechture. All globals are by default shared between all 'tasks', and so volatile is used there quite efficiently Zodiaq <zo...@ac...> wrote: Hello Louis and all Dev-C++ users, Thursday, June 19, 2003, 3:14:13 PM, Louis wrote as follows: LG> Can anyone explain the C keyword 'volatile' LG> as in the statement: volatile int x; LG> What does this do to variable x ? when does one need to use it ? volatile is very interesting keyword - it does nothing to variable but does something IMPORTANT to compiler optimization concering this variable... and what it does? It turns OFF optimizations like checking value of variable... normally variable value might be checked once in a loop (if it does not change during the loop) at first use but with volatile you force compiler to ALWAYS check it even if use it inside a loop... where is this keyword mainly used?? Hmmm... from my point of view its use is mainly useful in multithreaded application to check statesments that other threads might set - in few of my apps main thread launches child threads to do some work... this work is sometime very time consuming, so if user decides to stop this thread main thread sets one variable of child thread (lets say named Stopped) to true so its knows to stop working... before it returns it makes memory cleanup - frees memory used by thread... If I would just kill it (using TerminateThread) I'd get memory leaks - memory that child thread allocated isn't freed if it's stopped by TerminateThread... so child just checks this Stopped variable in few places (in loops of course) and if its value changes to true it knows to free memory and return... without volatile on Stopped thanks to compiler optimisations this child thread might never know that this value changed - child thread did not change it so why to check its value so often??? volatile does the job! I don't know about any other usages of volatile, but I'd gladly hear about some... -- regards, Zodiaq ------------------------------------------------------- This SF.Net email is sponsored by: INetU Attention Web Developers & Consultants: Become An INetU Hosting Partner. Refer Dedicated Servers. We Manage Them. You Get 10% Monthly Commission! INetU Dedicated Managed Hosting http://www.inetu.net/partner/index.php _______________________________________________ Dev-cpp-users mailing list Dev...@li... TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm https://lists.sourceforge.net/lists/listinfo/dev-cpp-users Thanx and Regards Avik Ghose SMS using the Yahoo! Messenger;Download latest version. |
|
From: Per W. <pw...@ia...> - 2003-06-19 14:13:37
|
The other use is basically the same. If the variable is updated by an interrupt routine, the main program must also declare the variable as volatile, to make sure that the compiler knows that the variable might change value outside of the control of the executing process/thread. /Per W On Thu, 19 Jun 2003, Zodiaq wrote: > Hello Louis and all Dev-C++ users, > > Thursday, June 19, 2003, 3:14:13 PM, Louis wrote as follows: > > LG> Can anyone explain the C keyword 'volatile' > LG> as in the statement: volatile int x; > > LG> What does this do to variable x ? when does one need to use it ? > > volatile is very interesting keyword - it does nothing to variable but > does something IMPORTANT to compiler optimization concering this > variable... and what it does? It turns OFF optimizations like checking > value of variable... normally variable value might be checked once in > a loop (if it does not change during the loop) at first use but with > volatile you force compiler to ALWAYS check it even if use it inside a loop... > where is this keyword mainly used?? Hmmm... from my point of view its > use is mainly useful in multithreaded application to check statesments > that other threads might set - in few of my apps main thread launches > child threads to do some work... this work is sometime very time > consuming, so if user decides to stop this thread main thread sets one > variable of child thread (lets say named Stopped) to true so its knows > to stop working... before it returns it makes memory cleanup - frees > memory used by thread... If I would just kill it (using > TerminateThread) I'd get memory leaks - memory that child thread > allocated isn't freed if it's stopped by TerminateThread... so child > just checks this Stopped variable in few places (in loops of course) > and if its value changes to true it knows to free memory and return... > without volatile on Stopped thanks to compiler optimisations this > child thread might never know that this value changed - child thread > did not change it so why to check its value so often??? volatile does > the job! I don't know about any other usages of volatile, but I'd > gladly hear about some... > > -- > regards, Zodiaq > > > > ------------------------------------------------------- > This SF.Net email is sponsored by: INetU > Attention Web Developers & Consultants: Become An INetU Hosting Partner. > Refer Dedicated Servers. We Manage Them. You Get 10% Monthly Commission! > INetU Dedicated Managed Hosting http://www.inetu.net/partner/index.php > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
|
From: Ioannis V. <iv...@em...> - 2003-06-19 16:23:54
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Louis Godio > Sent: Thursday, June 19, 2003 4:14 PM > To: DEVCppUsers > Subject: [Dev-C++] C keyword 'volatile' > > > Can anyone explain the C keyword 'volatile' > as in the statement: volatile int x; > > What does this do to variable x ? when does one need to use it ? It tells to the compiler that the value of the variable may change by an external factor (e.g. a thermometer) so it can make no assumptions about what its current value is, but always read it when its value is requested. Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |
|
From: Abhijit S. <mu...@gm...> - 2003-06-19 18:38:21
|
IV> "Foo." PW> "Bar." AG> "Rainwater harvesting." Zq> "Labamba." Yup, does all of that... and I'm feeling very left-out. It also prevents the virtual-memory sub-system from swapping the variable to disk, even if it's not used for a very long period of time. Windoze programmers will have no need for it in this context, though, since 'doze won't stay up very long, anyway... no, we should be bothered more about fflush()... <scampers off to save data before the next cra8h39hfhjug=0tuttt... ________________________________________ Abhijit Shylanath http://mudeth.tripod.com/ |
|
From: Ioannis V. <iv...@em...> - 2003-06-19 20:15:47
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Abhijit Shylanath > Sent: Thursday, June 19, 2003 9:43 PM > To: Dev-Cpp mailing list; Ioannis Vranos > Subject: RE: [Dev-C++] C keyword 'volatile' > > > > IV> "Foo." > PW> "Bar." > AG> "Rainwater harvesting." > Zq> "Labamba." > > Yup, does all of that... and I'm feeling very left-out. > > It also prevents the virtual-memory sub-system from swapping > the variable to > disk, even if it's not used for a very long period of time. Windoze > programmers will have no need for it in this context, though, > since 'doze > won't stay up very long, anyway... no, we should be bothered > more about > fflush()... <scampers off to save data before the next > cra8h39hfhjug=0tuttt... I am sorry but I do not understand what you mean with the above. :-) Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |
|
From: Mark L. <mli...@ip...> - 2003-06-20 01:10:13
|
On Thu, 19 Jun 2003 23:15:23 +0300 Ioannis Vranos <iv...@em...> wrote: > I am sorry but I do not understand what you mean with the above. :-) Me either. I think he had been using something else volatile before posting ;-) |
|
From: Ioannis V. <iv...@em...> - 2003-06-20 02:49:56
|
Ehehe, I agree. :-) Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net > -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Mark Livingstone > Sent: Friday, June 20, 2003 3:16 AM > To: dev...@li... > Subject: Re[2]: [Dev-C++] C keyword 'volatile' > > > On Thu, 19 Jun 2003 23:15:23 +0300 Ioannis Vranos > <iv...@em...> wrote: > > > I am sorry but I do not understand what you mean with the above. :-) > > Me either. I think he had been using something else volatile before > posting ;-) |
|
From: Abhijit S. <mu...@gm...> - 2003-06-20 18:28:31
|
> > fflush()... <scampers off to save data before the next > > cra8h39hfhjug=0tuttt... > > I am sorry but I do not understand what you mean with the above. :-) Interestingly (or poetic-justice-tically) enough, Windows did crash on me after that. I had downloaded a video and was trying to play it (in *WMP*, and it was in *WMV* format). -- Abhijit Shylanath Homepage: http://mudeth.tripod.com/ |