I'm trying to convert a single-threaded app to use multithreading (first time for me) and I immediately got punished for my sloppiness.
Code:
#include <iostream>
struct A
{
int n;
A(int n): n(n) {}
void foo() {
static int num = n;
std::cout << num << '\n';
}
};
int main()
{
A a(10), b(20);
a.foo();
b.foo();
}
Such statics are shared between objects which I thought would be completely independent but now that I have multiple threads it turns out they provide a back door to mess up each others internal state. (In singlethreading everything is OK, since the static has to remain stable only as long as the function call lasts.) Glad I was at least using asserts to catch problems with the internal state.
Guess I'll have to make them regular members of the class (or local variables).