DEV Community

voluntas
voluntas

Posted on

1 1

Erlang VM 上でユニークな数値を生成する

Erlang/OTP 18.x から erlang:now/0 が使えなくなった。では Erlang VM 上でのユニークな値をどうやって生成するのか。

方法

erlang:unique_integer/0,1 を使用する。

Erlang/OTP 18 [erts-7.2.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Eshell V7.2.1  (abort with ^G)
1> erlang:unique_integer([positive, monotonic]).
1
2> erlang:unique_integer([positive, monotonic]).
2
Enter fullscreen mode Exit fullscreen mode

このまま +1 ずつ加算されていく。この値はメモリが許す限り増えていく。

erlang:unique_integer/1 で positive と monotonic を指定することで、 Erlang VM 上で保証される連番が生成される。これは様々なプロセスから生成したとしてもユニークが保証されている。もちろん Erlang VM 上で、なので複数ノードとかでは保証されない。

もっと詳しく知りたい

詳細はこれを読めば良い。

Erlang/OTP 18 の新しい時刻関連 API とタイムワープモード - Qiita

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay