【问题标题】:erlang ets BIF implement ets_new_2 why do this?erlang ets BIF 实现 ets_new_2 为什么要这样做?
【发布时间】:2013-10-24 00:25:28
【问题描述】:

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

我的问题。为什么这样做? init_tb 只使用 common.memory_size 字段。为什么不使用 int 替换?

【问题讨论】:

    标签: erlang ets


    【解决方案1】:

    希望我能理解您的实际问题..

    从代码本身我们可以知道,erlang VM 是 SMP 模式(简单地说,多个内核上的多个 erlang 调度程序)。 在这种情况下,最好的方法(性能方面)是使用原子操作而不是锁。在内部,您指向的操作可能正在使用本机 CAS [http://en.wikipedia.org/wiki/Compare-and-swap](比较和交换)操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多