1
我在iOS開發人員角色的面試中被問到這個問題。如何在swift中使用協議實現併發線程?
// Please design a read-write task queue where you can tag the reader task with label,
// where the the task with the same label should be executed sequentially, and the
// tasks with different labels could be executed concurrently. However, the writer
// would get the exclusive access where no concurrent task would happen at the
// same time with the writer task
// For example:
protocol ConcurrentQueueWithSerialization {
// Submits a labeled task.
// All tasks with the same label will be serialized.
// Tasks with different labels will run concurrently.
// Use this method to submit a "read" operation from a particular reader.
func async(with label: String, task: @escaping() -> Void)
// Submits a task that will run concurrently with all other tasks regardless of their labels.
func async(task: @escaping() -> Void)
// Submits a labeled and delayed task.
func asyncAfter(deadline: DispatchTime, with label: String, task: @escaping() -> Void)
// Submits an unlabeled and delayed task.
func asyncAfter(deadline: DispatchTime, task: @escaping() -> Void)
// Submits a barrier task. Only one barrier task is allowed to run at a time.
// Works as a critical section for the queue.
// Use this method to submit a writer task.
func asyncBarrier(task: @escaping() -> Void)
}
class MyDispatchQueue: ConcurrentQueueWithSerialization {
//TODO: write your implementation
}
訪問者要求我在MyDispatchQueue類中實現上述協議。我試過但找不到解決方案。請幫幫我。提前致謝。
SO不是代碼寫入服務。你有什麼嘗試?你遇到了什麼問題? – Alexander
你的問題只包含要求 - 它並沒有顯示你方的努力來解決這個問題。請將您的嘗試加入這個問題 - 因爲本網站不是免費的「我們做您的(家)工作」服務。除此之外:請轉到[幫助]瞭解如何/在這裏問什麼。謝謝! – GhostCat