global variables in C++

In a C++ multi-threaded application with many classes, i am trying to find out what are the methods to define a global variable

  1. C style, define it as global in any one source file, define it as extern in a header which is included in the classes that access this variable.

  2. Write a Singleton class, which contains these global variables and exposes set/get methods to write to the variable.

By second method one can control multi-threaded access via locks in a centralized manner rather than the first approach.

Are there more and better ways?

5
задан Sashi 25 August 2010 в 16:02
поделиться