lua-users home
lua-l archive

Re: Upvalues vs C static variable

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> 在 2018年4月14日,上午12:24,Albert Chan <albertmcchan@yahoo.com> 写道:
> 
> 
>> On Apr 13, 2018, at 12:08 PM, 云风 <cloudwu@gmail.com> wrote:
>> 
>> CRT is thread-safe, but using a C static variable isn’t.
> 
> Do you mean C rand() does not use static variable ?
A thread-safe CRT may use TLS variable.
> 
>> When we run multi-states in multi-thread, calling math.rand() at the same time, the random sequence may rewind .
> 
> What is "may rewind" ?
> 
> 
The random algorithm may read the variable, do some calculation, and then save the new value to variable.
Consider two thread now,
thread 1 , calling rand() twice : At first, read A(seed) from mem -> B = f(A) -> return B and save it to mem, 
Second, read B from mem, f(B) -> C, return C and save it to mem.
thread 2, calling rand() at the same time: after reading A from mem, the thread is suspending , and then , it would save B to mem (after thread 1 changing the variable to C)
In this case, thread 1 ‘s random sequence will back to B again.

AltStyle によって変換されたページ (->オリジナル) /