1 //???????????????μ??????
2 class Singleton
3 {
4 public:
5     //get ????
6     static Singleton * getInstance(){
7         //??????????????????????????
8         lock();
9         //?ж? null
10         if (NULL == instance) {
11             //?ж???????????????????????????????????
12             instance = new Singleton();
13         }
14         //???????????
15         unlock();
16         //?????????????????
17         return instance;
18     }
19 private:
20     //?????????????
21     int count = 0;
22     //???????????????
23     static Singleton *instance;
24     //??й?????
25     Singleton(){
26         count++;
27         cout << "???????" << count << "??????" << endl;
28     }
29 };
30 //????? instance
31 Singleton * Singleton::instance = NULL;
??????????????? ab ???????????????????????????????????????????????????????????????????????a ????????????b ?????????? a????????????????a ????????????????????a ????????????? b ?????????????????????н??????????b ???? a ??????е?????????????????????b ????????????????????????????????????????????????????????
??????????????????μ????????????
?????????У???? get ?????У???? instance??????ж???????????ж???????????????????????????????????????????????????????????????????????ж????μ????????????????????????????????????????????????????t?????????????б????????????е?????????ж??????
?????????????????????д????????????????ж?????????????????????????????????????????????????????????????
????????ж????????????????????
1 class Singleton
2 {
3 public:
4     //get ????
5     static Singleton * getInstance(){
6         //???ж???? null????? null ???????????????????????????????????
7         if (NULL == instance) {
8             //??????????????????????????
9             lock();
10             //???????ж? null
11             if (NULL == instance) {
12                 //?ж???????????????????????????????????
13                 instance = new Singleton();
14             }
15             //???????????
16             unlock();
17         }
18                 //?????????????????
19         return instance;
20     }
21 private:
22     //?????????????
23     int count = 0;
24     //???????????????
25     static Singleton *instance;
26     //??й?????
27     Singleton(){
28         count++;
29         cout << "???????" << count << "??????" << endl;
30     }
31 };
32 //????? instance
33 Singleton * Singleton::instance = NULL;
????????????????????????????????????μ?Ч????????????????????????δ????????????????????????????????????????????????????ж????????Ч??