[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Upvalues vs C static variable
- From: Dirk Laurie <dirk.laurie@...>
- Date: Fri, 13 Apr 2018 17:01:56 +0200
2018-04-13 16:24 GMT+02:00 Albert Chan <albertmcchan@yahoo.com>:
> When to use lua upvalue mechanism instead of simpler C static variable ?
>
> Example, lua 5.4 work-1 math.random :
>
> It uses upvalue to store the 128-bits random state.
> Why not use static uint64_t state[2] ?
Otherwise the code is not reentrant.
Suppose two processes on Unix run Lua. Only one copy of the Lua shared
library gets loaded.
If a static variable is used, it gets updated when either process asks
for a random number. This violates the contract that the same seed
will buy you the same pseudo-random sequence. If an upvalue is used,
each process has its own independent pseudo-random sequence.