FlagHolder
Your turn with a Flag. Release it and the next thread in line goes.
Flag:PickupAsync gives one. Dead turns true once you call Release, and a second Release does nothing. When the work fits in a function, Flag:RunAsync is simpler: it releases even when the work errors.
Example
--!strict
-- PickupAsync gives a FlagHolder: the flag is yours until you Release it, then the next waiter gets it.
local Flag = require("@shotsystem/Flag")
local Promise = require("@shotsystem/Promise")
local flag = Flag.new()
local holder = assert(flag:PickupAsync())
print("holding:", flag.Taken, holder.Dead, tostring(holder))
print("a second pickup times out:", flag:PickupAsync(0.1))
-- this one waits its turn in a thread of its own
local waiting = Promise.Spawn(function()
local next = assert(flag:PickupAsync(5))
print("the waiter got it:", tostring(next))
next:Release()
end)
holder:Release()
holder:Release() -- a second Release does nothing
print("released:", holder.Dead, tostring(holder))
waiting:GetValueAsync(5)
print("free again:", flag.Taken)holding: true false FlagHolder a second pickup times out: nil the waiter got it: FlagHolder released: true FlagHolder(released) free again: false
Properties
| Name | Type | Default | Description |
|---|---|---|---|
| Dead | boolean | False while you hold the flag, true once Release is called. |
Methods
| Name | Arguments | Returns | Description |
|---|---|---|---|
| Release | () | () | Lets the next waiter in line take the flag; calling it a second time does nothing. |
