[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Upvalues vs C static variable
- From: 云风 <cloudwu@...>
- Date: Sat, 14 Apr 2018 00:55:11 +0800
> 在 2018年4月14日，上午12:24，Albert Chan <firstname.lastname@example.org> 写道：
>> On Apr 13, 2018, at 12:08 PM, 云风 <email@example.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.