What is triple lock control

Link: - "C ++ Modern Design: General Programming and Design Patterns" used by Andrei Alexandrescu Chapter 6 Introduction of Singletones.

Even if you install volatile, the Double Check safe and portable blocking is also not guaranteed. Why is this so?

If someone can put any good link that explains what the weakened memory model is and what exactly is the problem with the Double Check pattern. {Or someone can explain}

I used to think that volatile solved the problem, but it seems that it is not correct until I read the book.

+3
source share
3 answers

Even if you install volatile, the Double Check safe and portable blocking is also not guaranteed. Why is this so?

I will try to provide some context.

(Boehm McLaren) volatile ++, . Alexandrescu , ++ cajole , . . David Butenhof comp.programming.threads .

, (, Intel) volatile - , . .

, volatile . . Regehr et al.

, , , Java, , volatile, , , Double Checked Locking .

+8

, , . , .

"", , , . , .

+1

, " ", , , init 3 :    enum eINITIALIZATION_STATE {FIRST_INITIALIZER = 0, , };

( , , ! 30 SMP "" (HA!) . HW , , , ... !)

...

enum eINITIALIZATION_STATE {FIRST_INITIALIZER=0,INITIALIZING,INITIALIZED} ;

then you have an initialization control variable:

static int init_state;  //its 0 initialized 'cause static

then the idiom:

    IF(init_state == INITIALIZED)
        return your pointer or what ever;//the "normal" post initialization path
    ENDIF
    IF(init_state == FIRST_INITIALIZER)
        IF(compare_and_swap(&init_state,
                            INITIALIZING,
                            FIRST_INITIALIZER)==FIRST_INITIALIZER)

           //first initializer race resolved - I'm first.

           do your initialization here;

           //And now resolve the race induced by the compiler writers and the HW guys
           COMPILER_FENCE();//needs macro for portability
           HARDWARE_FENCE();//needs macro for portability

           //on intel using cas here supplies the HARDWARE_FENCE();
           compare_and_swap(&init_state,INITIALIZER,INITIALIZING);
           //now you can return your pointer or what ever or just fall through
       ENDIF
    ENDIF
    DOWHILE(*const_cast<volatile const int*>(&init_state)!=INITIALIZED)
        relinquish or spin;
    ENDDO
    return your pointer or what ever;

I’m not sure if this is what I had in mind, but because of the three conditions that I suspect might be equivalent (or at least similar) to what is meant by a “triple checked lock”.

+1
source

Source: https://habr.com/ru/post/1762855/


All Articles