[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Upvalues vs C static variable
- From: Albert Chan <albertmcchan@...>
- Date: Fri, 13 Apr 2018 11:35:47 -0400
> On Apr 13, 2018, at 11:01 AM, Dirk Laurie <email@example.com> wrote:
> 2018-04-13 16:24 GMT+02:00 Albert Chan <firstname.lastname@example.org>:
>> 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 ?
> Otherwise the code is not reentrant.
> Suppose two processes on Unix run Lua. Only one copy of the Lua shared
> library gets loaded.
Is this one copy shared library thing only for UNIX ?
> 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.
Prior to lua 5.4, lua math.random() use C rand().
rand() does not know about lua upvalues.
Does that mean the "old" lua math.random may obtain different
random output with the same seed ?