Macro to run code once during the lifetime of the program
It is not thread safe.  You can use Boost, or standard C++11 std::call_once.

Answer to comment:

Boost and C++11 are defining include library for launching threads and thread synchronization (locks, atomic variables…). The call_once function can either use those to ensure thread safety or use the thread lib of the OS (pthreads for *nix).

A very simplified (unefficent) implementation might be:

static std::mutex mutex;
static bool called = false;
{
   std::lock_guard&lt;std::mutex&gt; lock(mutex);
   if (!called) {
      f(); // &lt;- User code
      called = true;
   }
}


It is not very efficient because the lock is always taken. An optimisation it to atomically check the once flag before taking the mutex:

static std::mutex mutex;
static std::atomic&lt;bool&gt; called = false;
{
   if (!called) {
     std::lock_guard&lt;std::mutex&gt; lock(mutex);
     if (!called) {
        f(); // &lt;- User code
        called = true;
     }
   }
}


The implementation of pthread_once in glibc is interesting: it is much more complicated as it tries to behave correctly in presence of fork.

Boost/C++11 use a functor (function pointer or object with operator()): this way it can be implemented as a function and not a macro (you can use lambda in C++11 to avoid defining a separate function).