2013-10-16 43 views
1

R16B02 erl_db.c:1272erlang ets BIF實現ets_new_2爲什麼要這樣做?

/* we create table outside any table lock 
* and take the unusal cost of destroy table if it 
* fails to find a slot 
*/ 
{ 
    DbTable init_tb; 

    erts_smp_atomic_init_nob(&init_tb.common.memory_size, 0); 
    tb = (DbTable*) erts_db_alloc(ERTS_ALC_T_DB_TABLE, 
            &init_tb, sizeof(DbTable)); 
    erts_smp_atomic_init_nob(&tb->common.memory_size, 
          erts_smp_atomic_read_nob(&init_tb.common.memory_size)); 
} 

我的QU。爲什麼這樣做? init_tb只是使用common.memory_size字段。爲什麼不使用int替換?

回答

0

希望我理解您的實際問題..

正如我們可以從代碼本身就知道,那二郎神VM與SMP模式(簡單地說,在多核多二郎調度)。 在這種情況下,最好的方式(性能明智)是通過使用原子操作而不是鎖定。在內部,您指向的操作可能使用本地CAS [http://en.wikipedia.org/wiki/Compare-and-swap](Compare和Swap)操作。