|
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 > |