Boost logo

Boost :

From: Fabien Niñoles (fabien.ninoles_at_[hidden])
Date: 2007-02-05 15:37:40

> Quote from
> "Assigning to a static local variable is not thread safe and is not
> recommended as a programming practice."
> Although this isn't very concise wording (IIRC, the standard says that
> it should work for literals assigned to PODs) it seems clear that it
> won't work for non-PODs...

The example below the warning clearly show up what they mean, and it's
really a concurrency situation where two threads access the same object.
So, yes, this is a bad practice, and it is clearly not thread-safe, even for

// static3.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
struct C {
   void Test(int value) {
      static int var = 0;
      if (var == value)
         cout << "var == value" << endl;
         cout << "var != value" << endl;

      var = value;

int main() {
   C c1;
   C c2;

Output var != value
var == value

Boost list run by bdawes at, gregod at, cpdaniel at, john at