0

IF we use custom concurrent queue to resolve this issue why we use dispatch_sync for reading and dispatch_async with barrier for writing?

Can't we use dispatch_async for reading as well ?

What is the difference here?

srus2017
  • 394
  • 2
  • 14
  • Update your question with relevant code and clarify where your question is. – rmaddy Jun 28 '18 at 00:14
  • I guess `dispatch barrier` will make sure data chunks come down to the file in the right order. – RyanB Jun 28 '18 at 10:22
  • MY doubt is why we use dispatch_sync for read operation ? Why not dispatch_async – srus2017 Jun 28 '18 at 15:45
  • See https://stackoverflow.com/a/54101127/1271826. It’s Swift example, but all the basic principles are the same. – Rob Jan 09 '19 at 02:02

1 Answers1

0

Can't we use dispatch_async for reading as well ?

No, dispatch_async says “dispatch this block of code to run asynchronously, but don’t wait for it to finish”. But the purpose of the “reader” is to provide thread-safe access to the value and return that value. If you use dispatch_async, you may return back from your “reader” method before the dispatch_async block has a chance to run.

Rob
  • 415,655
  • 72
  • 787
  • 1,044