>  2005/02/27 (日) 09:16:16        [mirai]
> > Keyboard* Keyboard::Instance() {
> >     if (pInstance == 0) {
> >         Lock L(args);
> >         if (pInstance == 0) {
> >             pInstance = new Keyboard;
> >         }
> >     }
> >     return pInstance;
> > }
> > マルチスレッドだとこれにバグがありますヽ(´ー`)ノ
> Keyboard* Keyboard::Instance() {
> // 別スレッドもInstance()を呼んだ
> // まだなにもしていない
>     if (pInstance == 0) {
> // ここで別スレッドがLock L(args);した
>         Lock L(args); // 失敗:すでにロックされている
> // まだpInstanceが0のまま
>         if (pInstance == 0) { // 0なので真
> // ここで別スレッドがpInstanceに代入
>             pInstance = new Keyboard; // 上書き!
>         }
>     }
>     return pInstance;
> }
> でいいのか(´ー`)

それだとロック失敗だけで問題ないよヽ(´ー`)ノ
答えは pInstance = new Keyboard; のところでオブジェクトが作成される前に
pInstanceが0で無くなる可能性があるから

参考:2005/02/27(日)09時09分50秒