ETS Concurrency

Concurrency

        This module provides some limited support for concurrent access. All updates to single objects are guaranteed to be both atomic and isolated. This means that an updating operation towards a single object will either succeed or fail completely without any effect at all (atomicy). Nor can any intermediate results of the update be seen by other processes (isolation). Some functions that update several objects state that they even guarantee atomicy and isolation for the entire operation. In database terms the isolation level can be seen as "serializable", as if all isolated operations were carried out serially, one after the other in a strict order.
    
        ETS提供有限的并发访问。对单一对象的所有更新被保证是原子的和隔离的。这意味着对某一对象的一个更新操作要么是成功的,要么是失败的(原子)。这个更新操作的任何中间结果,其他进程都看不到(隔离)。有一些ets函数更新几个对象的状态,也会保证原子性和隔离性。如果使用数据库的术语,ETS的隔离级别是串行的,就好像这些隔离的操作被串行执行,按照严格的顺序,一个接一个。

        No other support is available within ETS that would guarantee consistency between objects. However, the safe_fixtable/2 function can be used to guarantee that a sequence of first/1 and next/2 calls will traverse the table without errors and that each existing object in the table is visited exactly once, even if another process (or the same process) simultaneously deletes or inserts objects into the table. Nothing more is guaranteed; in particular objects that are inserted or deleted during such a traversal may be visited once or not at all. Functions that internally traverse over a table, like select and match, will give the same guarantee as safe_fixtable.

        ETS没有其他的特性来支持多对象的一致性。然后, safe_fixtable  函数可以用来保证一系列的first,next调用遍历table时,不会产生错误;还可以保证表中的对象确切的被访问一次,即使另一个进程同时删除或插入对象。没有更多的保证了;特别的,在遍历过程中,插入或删除的对象,可能被访问到,也可能不。遍历table的函数,比如select和match,内部也使用了safe_fixtable。

        总结一下:ETS的并发基本可以忽略了。对单一对象的操作,是串行的;对多个对象的操作,没有原子性的保证,如果需要这个功能,应该使用mnesia。

read_concurrency

        {read_concurrency,boolean()} Performance tuning. Default is false. When set to true, the table is optimized for concurrent read operations. When this option is enabled on a runtime system with SMP support, read operations become much cheaper; especially on systems with multiple physical processors. However, switching between read and write operations becomes more expensive. You typically want to enable this option when concurrent read operations are much more frequent than write operations, or when concurrent reads and writes comes in large read and write bursts (i.e., lots of reads not interrupted by writes, and lots of writes not interrupted by reads). You typically do not want to enable this option when the common access pattern is a few read operations interleaved with a few write operations repeatedly. In this case you will get a performance degradation by enabling this option. The read_concurrency option can be combined with the write_concurrency option. You typically want to combine these when large concurrent read bursts and large concurrent write bursts are common.

        性能调优选项,默认为false。设置为true,优化了并发读操作。如果运行时系统有SMP支持,并且开启了这个选项,读操作变的更廉价;特别是有多个物理处理器的系统。然而, 读操作和写操作之间的切换变得更昂贵(读和写之间需要做到互斥)。在并发读操作比写操作更频繁,或者,爆发大量的读写操作(不被写中断的大量读操作,不被读中断的大量写操作)的情况下,你想开启这个选项。当少量的读操作被少量的写操作交错开的时候,开启这个选项是不合适的。开启这个选项,会降低系统性能。当系统会出现大量的读写爆发的时候,和write_concurrency 选项可以配合使用。

你可能感兴趣的:(concurrency,ets)