मेरे पास सी ++ 11 कोड का निम्न बिट है जो धागे और स्थैतिक परिवर्तनीय प्रारंभिकरण का उपयोग करता है। मेरा सवाल है:सी ++ स्थैतिक परिवर्तनीय inialization और धागे
सी ++ भाषा स्थिर चर के एकल प्रारंभिकरण के बारे में क्या गारंटी या आश्वासन देता है - नीचे दिया गया कोड सही मान प्रदर्शित करता है, हालांकि मुझे नए मानक में पारित होने का प्रतीत नहीं होता है जो बताता है कि कैसे स्मृति मॉडल धागे के साथ बातचीत करनी चाहिए। जब कभी चर चर-स्थानीय बन जाते हैं?
#include <iostream>
#include <thread>
class theclass
{
public:
theclass(const int& n)
:n_(n)
{ printf("aclass(const int& n){}\n"); }
int n() const { return n_; }
private:
int n_;
};
int operator+(const theclass& c, int n)
{
return c.n() + n;
}
void foo()
{
static theclass x = 1;
static theclass y = x + 1;
printf("%d %d\n",x.n(),y.n());
}
int main()
{
std::thread t1(&foo);
std::thread t2(&foo);
t1.join();
t2.join();
return 0;
}