Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to choose serialQueue over concurrent queue in ios

I was asked to implement a thread safe dictionary in swift, I used the common approach:

 class MutableDictionary {
    var dictionary: [String : Any] = [:]
    var queue = DispatchQueue(label: "queue", attributes: .concurrent)
    
    func object(for key:  String) -> Any? {
        queue.sync {
            return dictionary[key]   
        }
    }
    
    func set(_ object: Any?, for key: String) {
        queue.async(flags: .barrier) {
            self.dictionary[key] = object
        }
    }
}

However, the following up question is:

  1. What's the difference with using concurrent + barrier vs just using a serialQueue for setting in this case?
  2. I did a testing on playground, I wrapped the get and set with 1000 time for loop, it turns out that the behavior for both serial and concurrent queue is almost the same.
  3. Why the setting always raise an error? enter image description here
  4. What does concurrent queue do better in this case (for both get and set) compared to a serial queue?
  5. In iOS, when should I choose serial queue over concurrent queue? And vice-versa?
like image 747
RobotX Avatar asked Dec 06 '25 10:12

RobotX


2 Answers

1、with barrier, the concurrent queue is temporary able to execute one task at a time. enter image description here

However, the serialQueue can only perform one task at a time.

2、Given you only use the queue to read/write, they obviously have the same effect. if you put another kind task to it, apparently the concurrent queue cost less and that is the real difference.

3、Submitted to async object address will be defined when your object is a class. You will have a bad address when you give the class member a new value, So you cannot access it and the error comes. You can have a try by struct .

4、refer to answer 1.

5、Sometimes when you want the task to be performed faster, concurrentQueue first. If you want to execute tasks orderly, serialQueue do the better.

like image 162
Elevo Avatar answered Dec 08 '25 22:12

Elevo


  1. concurrent + barrier can run multiple read at the same time. serial queue can only run one task (read/write) at a time.

  2. the results are the same, or even that the serial queue is better because you're using only one thread to run. You can only take advantage of the concurrent + barrier implementation when read/write operations happen on multiple thread/queue. In this case, the serial queue is better because you don't need to look and switch between queue/thread.

  3. Full source code, please?

  4. Concurrent + barrier might be better or not, as in (2), sometimes if you can ensure all operations happen in the same thread, then serial queue is better.

  5. It depends on your case, as mentioned in (2), (4). One more thing about concurrent + barrier, sometimes it isn't a good choice as you think. Imagine:

  • You're implementing a feature that needs to do a heavy write operation, for example, you're reading your dict and calculating a new value, and updating the dict. You wrap all of these steps in a queue.async(flags: .barrier) block.

  • You expect these steps are running in your thread (background thread) and it doesn't block the main queue from reading the dict to update UI. But it does block the main queue, right? Read operations from the main queue have to wait for barrier block to finish first.

  • If your application consumes a lot of CPU, you may have to wait for OS to find the thread for your update steps, which means you have to spend more time on it.

like image 25
nghiahoang Avatar answered Dec 08 '25 22:12

nghiahoang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!