AW: [En-Nut-Discussion] global variable

Ralph Mason ralph.mason at telogis.com
Mon Aug 14 07:39:45 CEST 2006


You really don't need to mark your global volatile in NutOS unless you can
also access that variable from a IRQ routine.

Because Nut is cooperative then a context change will always occur during a
function call.  The compiler will not optimize access to a variable across
function calls.  Thus it's safe to use non volatile globals.  If it were a
preemptive OS then you would need that.

Ralph


-----Original Message-----
From: en-nut-discussion-bounces at egnite.de
[mailto:en-nut-discussion-bounces at egnite.de] On Behalf Of Vesa Jääskeläinen
Sent: Saturday, August 12, 2006 10:09 PM
To: Ethernut User Chat (English)
Subject: Re: AW: [En-Nut-Discussion] global variable

Hi,

You might want to declare your variable as volatile so compiler won't
optimize some cases out.

In example:
volatile int global_var;

Thanks,
Vesa Jääskeläinen

Ernst Stippl wrote:
> Hi!
> 
> I am not sure if I understood your question, but here is what I can offer:
> 
> Have a look at the Ethernut Sample program "threads.c":
> 
> Insert a variable definition between the "include" and the thread
> definition:
> 
> #include <sys/thread.h>
> #include <sys/timer.h>
> 
> int global_var;            // <-- insert here
> 
> /*
>  * High priority thread.
>  */ 
> 
> As far as I can see, the variable global_var will be accessible by both
> threads, they can read/write it. But you have to invent a set of rules to
> make sure access to the variable to done in an orderly fashion. Otherwise
> both threads MAY write to the variable and so changes to it could get
lost.
> 
> regard
> ernst 
> 
> 
> -----Ursprüngliche Nachricht-----
> Von: en-nut-discussion-bounces at egnite.de
> [mailto:en-nut-discussion-bounces at egnite.de] Im Auftrag von Raul Valle
> Gesendet: Samstag, 12. August 2006 03:45
> An: en-nut-discussion at egnite.de
> Betreff: [En-Nut-Discussion] global variable
> 
> hi all , how can i use one variable that is able to work for all threads,
> that if i modify its value one one thread , in the other can reflect that
> change _______________________________________________
> En-Nut-Discussion mailing list
> En-Nut-Discussion at egnite.de
> http://www.egnite.de/mailman/listinfo.cgi/en-nut-discussion
> 
> 
> 
> --
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.1.405 / Virus Database: 268.10.9/417 - Release Date: 11.08.2006
> 
> 
> 

_______________________________________________
En-Nut-Discussion mailing list
En-Nut-Discussion at egnite.de
http://www.egnite.de/mailman/listinfo.cgi/en-nut-discussion




More information about the En-Nut-Discussion mailing list