0

I have a method which gets called when receiving data from the network. Just out of curiosity I kept a counter to check if the async calls to the same thread are synchronous. I know its dumb, but I found something strange.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSLog(@"counterForAsync=%d",counterForAsync);
            ++counterForAsync;
            if (file)  {
                [file seekToEndOfFile];
            }
            [file writeData:data];
});

I copied the console log and observed it in sublime, and saw that some line were repeated.

enter image description here

enter image description here

same thing in on a few other lines. If it is running twice then its bad because I am performing file operation in my async thread.

2
  • 2
    "async calls to the same thread" ... They're to the same queue, not the same thread. A global queue is runs dispatched code on any of a number of worker threads, and code on these various threads can run concurrently with respect to each other. Create your own serial dispatch queue, and you'll experience the behavior you were expecting. Or, a little more advanced, if these calls can happen more frequently than the target queue can process them, you can also use a dispatch add source to coalesce them together. Commented Sep 5, 2018 at 15:54
  • 1
    I think yours experiment proved that code isn't invoked in the same thread and isn't synchronous. Commented Sep 5, 2018 at 16:07

1 Answer 1

4

There are two types of dispatch queues.

  • serial
  • concurrent

A serial queue(like the main queue) executes the enqueued closures "in order" that you enqueue, one at a time.

However, a concurrent queue(like global dispatch queue) executes the closures concurrently. (Take a look at the Apple documentation)

In those cases where the same counter number is printed twice, 309th closure NSLogged the counter before 308th closure added 1 to the counter. (Do you get it?)

So for the file write operation you should use serial queue like below, as the documentation states

Serial queues are often used to synchronize access to a specific resource.

dispatch_queue_t serialQueue = dispatch_queue_create("com.myqueue.filewrite", DISPATCH_QUEUE_SERIAL);

dispatch_async(serialQueue, ^{
    NSLog(@"counterForAsync=%d",counterForAsync);
        ++counterForAsync;
        if (file)  {
            [file seekToEndOfFile];
        }
        [file writeData:data];
});

FYI, this is called synchronization. You should synchronize your code so that your critical section(like adding a counter or writing to a file) is not accessed by more than two threads at the same time.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you @sj-r... I will look into it and then accept it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.