【问题标题】:How can we implement concurrency thread using protocol in swift? [closed]我们如何在swift中使用协议实现并发线程? [关闭]
【发布时间】:2017-08-11 05:25:31
【问题描述】:

我在 iOS 开发者职位的面试中被问到这个问题。

// 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 不是代码编写服务。你试过什么?你遇到了什么问题?
  • 您的问题仅包含要求 - 它并未显示您为自己解决此问题所做的任何努力。请在此问题中添加您的尝试 - 因为该站点不是免费的“我们为您提供(家庭)工作”服务。除此之外:请转至help center 了解如何/在这里问什么。谢谢!

标签: ios swift multithreading protocols


【解决方案1】:

之前我建议使用目标队列,但更好的是,创建一个主并发队列,然后为命名队列创建串行队列,然后通过该主并发队列调度所有内容。与目标队列方法不同,这将尊重分派到命名队列的任务调度与分派到未命名队列的任务。

使用该实现,这是一个示例(Instruments“兴趣点”配置文件),其中我为名为“fred”和“ginger”的队列添加了任务,还添加了一个添加到未命名队列的任务,然后我添加了一个屏障任务,然后在上述每个队列中再添加两个任务。

如您所见,它尊重命名队列的串行性质,未命名队列是并发的,并且所有这些队列相对于彼此是并发的,但屏障是所有队列之间的屏障。

class MyDispatchQueue: ConcurrentQueueWithSerialization {
    private var namedQueues = [String: DispatchQueue]()
    private var queue = DispatchQueue(label: Bundle.main.bundleIdentifier! + ".target", attributes: .concurrent)
    private let lock = NSLock()

    private func queue(with label: String) -> DispatchQueue {
        lock.lock()
        defer { lock.unlock() }

        if let queue = namedQueues[label] { return queue }

        let queue = DispatchQueue(label: Bundle.main.bundleIdentifier! + "." + label)
        namedQueues[label] = queue
        return queue
    }

    func async(with label: String, task: @escaping () -> Void) {
        queue.async {
            self.queue(with: label).sync(execute: task)
        }
    }

    func async(task: @escaping () -> Void) {
        queue.async(execute: task)
    }

    func asyncAfter(deadline: DispatchTime, with label: String, task: @escaping () -> Void) {
        queue.asyncAfter(deadline: deadline) {
            self.queue(with: label).sync(execute: task)
        }
    }

    func asyncAfter(deadline: DispatchTime, task: @escaping () -> Void) {
        queue.asyncAfter(deadline: deadline, execute: task)
    }

    func asyncBarrier(task: @escaping () -> Void) {
        queue.async(flags: .barrier, execute: task)
    }
}

注意,我还同步访问namedQueues数组,以保证这个类的线程安全。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多