简体   繁体   中英

erlang ets BIF implement ets_new_2 why do this?

R16B02 erl_db.c:1272

/* 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));
}

My Qus. why do this? the init_tb just use common.memory_size field. why not use a int replace?

Hope that I have understood your actual question..

As we can get to know from the code itself, that the erlang VM is with SMP mode (to put it simply, multiple erlang schedulers on multiple cores ). In such cases, the best way to do it (performance wise) is by using atomic operations instead of locks. Internally the operation you are pointing may be using native CAS [http://en.wikipedia.org/wiki/Compare-and-swap](Compare And Swap) operations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM