Can dispatch_async interrupt a task call

Suppose I send a task asynchronously to a queue:

{
    // we are on main queue

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
      print("task B")
    });

    print("task A")

    // some long running work

    print("task A")

    // some long running work

    print("task A")
    ...
}

In any case, can a submitted task interrupt the task from which it was sent? In other words, can it ever happen that “task B” is printed before all “tasks A” are printed?

task A
task B
task A
task A
...

Or GCD guarantees this:

task A
task A
task A
...
task B

Will it matter if the task was sent to the same queue from which dispatch_async is called?

+4
source share
3 answers

In other words, can it ever happen that “task B” is printed before all “tasks A” are printed?

. (a) ; (b) ( - ), , .

A B - , , .. , B , A, ( , , ..).

, , , .

+5

, - , " B" , " A"?

, , - . , "-, " (, ).

dispatch_async , , , : , . ( DISPATCH_QUEUE_PRIORITY_DEFAULT .)

+1

, "As" "B", :

// we are on main queue

print("task A")

// some long running work

print("task A")

// some long running work

print("task A")
...

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), 
^{
  print("task B")
});

, , As - , B - .

Sorry, it seems to me that you did not understand a bit what GCD is for. Maybe you want to tell us what your actual concrete scenario is and what you want to achieve in this scenario.

0
source

Source: https://habr.com/ru/post/1599665/


All Articles