Test and Set a Value Atomically
ok = atomic.testandset(t, idx, vt, vs)
Test and set a table index value atomically.
Parameters | Type | Description |
---|---|---|
t |
table | Table to modify |
idx |
string | The table index to check and set |
vt |
integer | The value to test for |
vs |
integer | The value to set |
Returns ok == true
if the t[idx] == vt
, and then t[idx] = vs
.
Equivalent to:
function atomic.testandset(t, idx, vt, vs)
if t[idx] ~= vt then return false end
t[idx] = vs
return true
end
(But an internal mutex ensures that this happens atomically - i.e. the state is coherent even if multiple threads perform the same operation)