I have an array that can be accessed and modified by multiple players at the same time.
How to make that tread safe?
Array is a member of a particular class that implements function that is being called by players.
It depends.
The documentation says that anything that does not use <suspends>
is already atomic (even when it runs in parallel with other code).
Immediate expressions stick together on their own. All adjacent immediate (non-async) expressions are considered to be atomic — their code is guaranteed to run without interruption within the same update, and without preemption or context switching. It is as though such code had an automatic mutual-exclusion primitive wrapped around them.
Let’s take this code that tries to avoid duplicates:
if (not Array.Find[Value]):
set Array += array { Value }
In Verse this not need extra safety (such as locks) that it would need in other languages.
If you do suspend, that’s no longer true:
if (not Array.Find[Value]):
Sleep(0.0)
set Array += array { Value }
However there is no general answer: it will depend on your code structure, and array operations you need to do.
For example, in the above scenario you could check Find
again after sleeping.
Thanks, this is answer I was looking for.
Can you please just point me where in documentation did you find this?
Sure: please see Concurrency Overview.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.