godot-start/zfoo/util/IdUtils.gd

27 lines
503 B
GDScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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