1

People are asking what is the way to run task in an thread. One of the answer accepted by many people(from the link) is to use GCD.

So, I tried in this way by printing out thread id to see if the task is really executed in another thread after put into GCD queue:

// a function in MyService class    
- (void) doTask {
        NSLog(@"start do task on thread = %@", [NSThread currentThread]);
        dispatch_queue_t queue = dispatch_queue_create("com.company.myqueue", DISPATCH_QUEUE_SERIAL);
        dispatch_sync(queue, ^{
            NSLog(@"execute task on thread = %@", [NSThread currentThread]);
        });
    }

I run :

// this is not main thread [myService doTask]

The console output is this:

start do task on thread = <NSThread: 0x15b4be00>{number = 6, name = (null)}
execute task on thread = <NSThread: 0x15b4be00>{number = 6, name = (null)}

It looks like the task is NOT executed in another thread, it is on the same thread as the caller thread. Do I misunderstand something? Why GCD doesn't execute task in a separate thread but many pepole accept the answer in that link?

Community
  • 1
  • 1
Leem.fin
  • 35,699
  • 70
  • 166
  • 297

3 Answers3

1

GCD optimizes when it can. Running a block synchronously never requires switching threads, with the sole exception of dispatching to the main queue, as that must run on the main thread.

Avi
  • 7,150
  • 1
  • 18
  • 22
0

Try below code which i have taken from the Accepted answer which you have mentioned in your question.

   dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // Add code here to do background processing
    //

     NSLog(@"start do task on thread = %@", [NSThread currentThread]);


    dispatch_async( dispatch_get_main_queue(), ^{
        // Add code here to update the UI/send notifications based on the
        // results of the background processing

         NSLog(@"execute task on thread = %@", [NSThread currentThread]);
    });
});

It will print both different thread. In your test case which you have put in your question, Thread is same and it is synchronous approach. So, it is printing same thread. It is very broad concept to explain everything here. You should refer Apple Documentation for GCD.

Documents states about dispatch_sync,

Submits a block to a dispatch queue for synchronous execution. Unlike dispatch_async, this function does not return until the block has finished. Calling this function and targeting the current queue results in deadlock.

Unlike with dispatch_async, no retain is performed on the target queue. Because calls to this function are synchronous, it "borrows" the reference of the caller. Moreover, no Block_copy is performed on the block.

> As an optimization, this function invokes the block on the current thread when possible.

Community
  • 1
  • 1
Ketan Parmar
  • 25,426
  • 9
  • 43
  • 67
0

Check this

https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/dispatch_get_global_queue.3.html

It says that

Queues are not bound to any specific thread of execution and blocks submitted to independent queues may execute concurrently.

So thread selection of performing task is decide by thread pulling algorithms used by dispatch lib. but it give guarantees that queues execution is done by given configuration of queue(serial or concurrent and other)

ERbittuu
  • 922
  • 8
  • 19