1

I need to use GCD dispatch_sync in my class method. But as I understand it, in order to make things thread safe I need reference the same queue in each class method? If so in the following code, I cannot get access to _concurrentQueue as that would be in an instance of this class, but this calls has no instance methods (i.e. All static methods). If I create a new dispatch queue with the same label (i.e. com.company.cacheConcurrentQueue) will that use the same queue if I call doBigThings and doOtherThings at the same (well one right after the other)?

_conQueue = dispatch_queue_create("com.company.cacheConQueue",
                                             DISPATCH_QUEUE_CONCURRENT);
 +doBigThings {

   dispatch_sync(_conQueue, ^{
      do something...
   });

 }

 +doOtherThings {

   dispatch_sync(_conQueue, ^{
      do something...
   });

 }
0

1 Answer 1

0

First, I'd check very carefully whether you really want to use dispatch_sync. dispatch_sync waits for the block to finish executing, so you should only ever do this from a background thread.

Now all you need to do is write a function (just typing it, so you need to make it compile)

static void dispatch_on_my_queue (dispatch_block_t block)
{
    static dispatch_queue myQueue;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        my_queue = dispatch_queue_create (...);
    });

    dispatch_sync (my_queue, block);
}

and call it from anywhere.

Not the answer you're looking for? Browse other questions tagged or ask your own question.