我今天实现了一个信号灯,它引发了一些有关信号灯,线程和队列的问题。我的以下表示法正确吗?
let semaphore = DispatchSemaphore(value: 1)
let serialQueue = DispatchQueue(label: "serial")
someAsyncMethod {
serialQueue.async {
// When someAsyncMethod returns, this queue, behind the scenes,
// creates a Thread/NSThread object and approaches the
// semaphore and it is this thread object that decrements
// the semaphore counter.
// The next time someAsyncMethod returns, a new thread object
// is created behind the scenes (by the queue) and this thread
// object is made to wait until something (anything) signals
// it is done.
semaphore.wait()
// do work...
someMethod()
}
}
func someMethod() {
// do work...
// This task may ultimately be on a different thread than
// where it started despite being in the same queue
// (since not every task is guaranteed to run on the same thread)
// but it doesn't matter, any object can signal the semaphore.
semaphore.signal()
}
someAsyncMethod
返回并进入队列时是否创建了新的线程对象/实例?信号量不是特定于线程的。重点是要在线程之间进行协调,因此它们必须可供多个线程使用。
在这种特定情况下,不需要信号量(如果只有这些用途),因为您正在使用串行队列。根据定义,串行队列仅允许队列中的一个任务一次以先进先出的顺序运行。也就是说,第二个任务不需要等待信号量就可以避免与第一个任务同时运行,因为在第一个任务完成之前它甚至不允许启动。
[当您异步将任务放入队列时,调用代码可以立即继续。任务更像是将数据对象放入数据结构(队列)中。 Grand Central Dispatch使用线程池将任务弹出队列并执行。是否必须创建新线程取决于池中是否已经有足够的空闲线程。