godot-start/zfoo/util/IdUtils.gd

27 lines
503 B
GDScript
Raw Normal View History

2021-10-04 08:49:12 +00:00
extends Object
const atomicInt: Array = [0]
const mutex = [false, null]
static func getMutex() -> Mutex:
if mutex[0]:
return mutex[1]
var mutexInstance = Mutex.new()
mutex[1] = mutexInstance
return mutexInstance
# 获取本地int的唯一id如果达到最大值则重新从最小值重新计算线程安全
static func getLocalIntId() -> int:
var mutexInstance = getMutex()
mutexInstance.lock()
var id = atomicInt[0]
atomicInt[0] = id + 1
mutexInstance.unlock()
return id