|
Boost : |
From: Fabien Niñoles (fabien.ninoles_at_[hidden])
Date: 2007-02-05 15:37:40
>
> Quote from http://msdn2.microsoft.com/en-us/library/s1sb61xd.aspx:
> "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
POD:
// 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;
else
cout << "var != value" << endl;
var = value;
}
};
int main() {
C c1;
C c2;
c1.Test(100);
c2.Test(100);
}
Output var != value
var == value
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk