Does NSD () expect NSG dispatch_async?

From what I read about Grand Central Dispatch, GCD does not do proactive multitasking; this is just one cycle of events. I have problems with this output. I have two queues that just make some conclusions (at first I read / wrote some general condition, but I was able to simplify this and still get the same result).

dispatch_queue_t authQueue = dispatch_queue_create("authQueue", DISPATCH_QUEUE_SERIAL); dispatch_queue_t authQueue2 = dispatch_queue_create("authQueue", DISPATCH_QUEUE_SERIAL); dispatch_async(authQueue, ^{ NSLog(@"First Block"); NSLog(@"First Block Incrementing"); NSLog(@"First Block Incremented"); }); dispatch_async(authQueue, ^{ NSLog(@"Second Block"); NSLog(@"Second Block Incrementing"); NSLog(@"Second Block Incremented"); }); dispatch_async(authQueue2,^{ NSLog(@"Third Block"); NSLog(@"Third Block Incrementing"); NSLog(@"Third Block Incremented"); }); 

I get the following output:

 2011-12-15 13:47:17.746 App[80376:5d03] Third Block 2011-12-15 13:47:17.746 App[80376:1503] First Block 2011-12-15 13:47:17.746 App[80376:5d03] Third Block Incrementing 2011-12-15 13:47:17.746 App[80376:1503] First Block Incrementing 2011-12-15 13:47:17.748 App[80376:1503] First Block Incremented 2011-12-15 13:47:17.748 App[80376:5d03] Third Block Incremented 2011-12-15 13:47:17.750 App[80376:1503] Second Block 2011-12-15 13:47:17.750 App[80376:1503] Second Block Incrementing 2011-12-15 13:47:17.751 App[80376:1503] Second Block Incremented 

As you can see, blocks are not executed atomically. My only theory is that writing a GCD to stdio via NSLog makes the current execution wait. I can not find anything related to this in the Apple documentation. Can anyone explain this?

+4
source share
4 answers

GCD does not use any kind of "event loop". This is a new kernel feature in recent releases of Mac OS X and iOS; in fact, I don’t have any other similar technology that I know of.

The goal is to complete the execution of all the code that you pass as fast as the hardware allows. Please note that it targets the fastest end time, not the fastest start time. Subtle difference, but important with real impact on the world, how it works.

If you have only one idle processor core, theoretically only one of them will run at a time. Since multitasking inside one core is slower than performing two tasks in sequence. But actually it is not. If the CPU core becomes inactive or not very busy for some time (for example, reading a hard disk or waiting for a response from some other program (Xcode draws the output of NSLog)), then it will most likely go on to execute for a second GCD, because the one he's doing now is stuck.

And, of course, most of the time you will have more than one idle processor core.

He also will not necessarily do things in the order in which you give it. The GCD / core controls these details.

In your specific example, the Xcode debugger is probably able to handle only one NSLog() event at a time (at least it should do the drawing one at a time). You have two queues and they can start execution at the same time. If you send two NSLog() statements at once, one of them will wait for the other to finish first. Since you do nothing but print material in Xcode, these two GCD queues will be the first to race to send log data to Xcode. The first has a small initial start, but it is very small and often not enough for it to first open a connection to Xcode.

It all depends on what actual hardware resources are available on the hardware for that particular nanosecond in time. You cannot predict it, and you need to properly structure your lines in order to take some control.

+8
source

Where did you read that GCD doesn't do proactive multitasking? I think you're wrong. It is built on the threading support provided by the system, and therefore GCD blocks sent to the queue can be preventively interrupted.

The behavior you see is exactly what I expect. The first and second blocks are sent in the same queue, so the GCD ensures that the first block is completed before the second block starts. However, the third block is sent to a completely different queue (i.e., it will be executed in a separate background thread), and therefore its output is alternated with two other blocks, since the threads are planned by the system.

+2
source

Everything that you read is incorrect, if you do not use the next dispatch queue, all blocks will be executed simultaneously.

+2
source

Your queues work in 2 matching background threads. They provide NSLog messages. Although one thread concludes NSlog, another is waiting.
What's wrong?

0
source

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


All Articles