[ back to toc ]

Static variables in multithreaded code

Date: 2002/05/30 11:54

I am fairly new to C/C++. I am trying to have a few global variables
(maybe static?) including an STL list variable that will be used (items
added/deleted) withing multi threads. The OS is AIX 4.3 and the compilar
is g++ (GNU) 2.9.

How best can I do this? I mean should I explicitly define these global
varibles as 'static' (if so how do I do it?). Or should I use the 'extern'
keyword in defining these global varibles?

It'll be a great help if you can give me a code example on how to define
these global varibles according to your recommendation.

The keyword static and extern have special meaning when using for
declaration of global variables.

Declaring a global variable 'static' means that the symbolic reference of
the variable (the variable name) will not get into the object file symbol
table and thus the variable can not be referenced from other source file
linked to the final executable. Other than this there is no other effect.
This can help you to use global variables that are local to some 'module'
implemented in a single source file.

The keyword 'extern' tells the compiler that the variable is declared
somewhere else. Thus if a variable is declared as 'extern' in each C
source file and not without this keyword then the linker will complain
that the variable is referenced but not defined. It has to be defined in
one of the source files.

I hope this will make these issues more clear.


[ back to toc ]