, " ", , , 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;
then the idiom:
IF(init_state == INITIALIZED)
return your pointer or what ever;
ENDIF
IF(init_state == FIRST_INITIALIZER)
IF(compare_and_swap(&init_state,
INITIALIZING,
FIRST_INITIALIZER)==FIRST_INITIALIZER)
do your initialization here;
COMPILER_FENCE();
HARDWARE_FENCE();
compare_and_swap(&init_state,INITIALIZER,INITIALIZING);
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”.
source
share