concurrencygrand-central-dispatchserial-processing

Concurrent or Serial queue for Reading or Writing


I'm writing an NSObject subclass similar to that of NSCache, which enables file caching to the disk on an iOS Device.

I am in the process of writing the queues for (i) reading (ii) writing, but I wanted to make sure that the type of queue I will be creating is right, and won't therefore cause problems with files becoming corrupt in the future.

For the read queue, I was planning on creating a concurrent queue as many files can be read at the same time without any issue.

For the write queue, however, I was planning on creating a serial queue to prevent more than one file being written to at once.

Can you tell me if this is the correct approach?


Solution

  • Based on your follow-comment, I think the pattern / usage you're looking for is a serial queue (one per cache object / file) with dispatch_async()'d blocks for writing cache entries and dispatch_sync() (note difference) for reading them. The writes can be asynchronous on the serial queue, which will still keep them ordered, and doing synchronous reads will force all pending writes to be completed before trying to read a value back out.