Episode 1
for (int i =0;i<10;i++)
{
NSLog (@ "i=%d", I);
dispatch_queue_t myserialqueue = dispatch_queue_create ("com.biostime.xxx", NULL);
__block int d = i;
Dispatch_async (Myserialqueue, ^{
Nsrunloop *loop = [Nsrunloop currentrunloop];
NSLog (@ "%p excute1 i =%d d=%d", loop,i,d);
d=9;
NSLog (@ "%p excute2 i =%d d=%d", loop,i,d);
});
NSLog (@ "i=%d", I);
}
=============
2015-05-25 23:15:10.758 shareobjectdemo[4165:100897] I=0
2015-05-25 23:15:10.760 shareobjectdemo[4165:100897] I=0
2015-05-25 23:15:10.760 shareobjectdemo[4165:100999] 0x7face9612c80 excute1 i = 0 d=0
2015-05-25 23:15:10.760 shareobjectdemo[4165:100897] I=1
2015-05-25 23:15:10.761 shareobjectdemo[4165:100897] I=1
2015-05-25 23:15:10.761 shareobjectdemo[4165:100999] 0x7face9612c80 excute2 i = 0 d=9
2015-05-25 23:15:10.761 shareobjectdemo[4165:100897] i=2
2015-05-25 23:15:10.762 shareobjectdemo[4165:100897] i=2
2015-05-25 23:15:10.762 shareobjectdemo[4165:100999] 0x7face9612c80 excute1 i = 1 d=1
2015-05-25 23:15:10.762 shareobjectdemo[4165:100897] I=3
2015-05-25 23:15:10.762 shareobjectdemo[4165:100999] 0x7face9612c80 excute2 i = 1 d=9
2015-05-25 23:15:10.763 shareobjectdemo[4165:100897] I=3
2015-05-25 23:15:10.763 shareobjectdemo[4165:100999] 0x7face9612c80 excute1 i = 3 d=3
2015-05-25 23:15:10.763 shareobjectdemo[4165:100897] i=4
2015-05-25 23:15:10.763 shareobjectdemo[4165:100999] 0x7face9612c80 excute2 i = 3 d=9
2015-05-25 23:15:10.764 shareobjectdemo[4165:100897] i=4
2015-05-25 23:15:10.762 shareobjectdemo[4165:101000] 0x7face94950e0 excute1 i = 2 d=2
2015-05-25 23:15:10.764 shareobjectdemo[4165:100999] 0x7face9612c80 excute1 i = 4 d=4
2015-05-25 23:15:10.764 shareobjectdemo[4165:100897] i=5
2015-05-25 23:15:10.764 shareobjectdemo[4165:101000] 0x7face94950e0 excute2 i = 2 d=9
2015-05-25 23:15:10.765 shareobjectdemo[4165:100999] 0x7face9612c80 excute2 i = 4 d=9
2015-05-25 23:15:10.787 shareobjectdemo[4165:100897] i=5
2015-05-25 23:15:10.788 shareobjectdemo[4165:101000] 0x7face94950e0 excute1 i = 5 d=5
2015-05-25 23:15:10.788 shareobjectdemo[4165:100897] I=6
2015-05-25 23:15:10.789 shareobjectdemo[4165:100897] I=6
2015-05-25 23:15:10.789 shareobjectdemo[4165:101000] 0x7face94950e0 excute2 i = 5 d=9
2015-05-25 23:15:10.790 shareobjectdemo[4165:100897] i=7
2015-05-25 23:15:10.790 shareobjectdemo[4165:100897] i=7
2015-05-25 23:15:10.790 shareobjectdemo[4165:100998] 0x7face9614150 excute1 i = 6 d=6
2015-05-25 23:15:10.791 shareobjectdemo[4165:100897] I=8
2015-05-25 23:15:10.791 shareobjectdemo[4165:100998] 0x7face9614150 excute2 i = 6 d=9
2015-05-25 23:15:10.791 shareobjectdemo[4165:100897] I=8
2015-05-25 23:15:10.791 shareobjectdemo[4165:101000] 0x7face94950e0 excute1 i = 7 d=7
2015-05-25 23:15:10.792 shareobjectdemo[4165:100897] I=9
2015-05-25 23:15:10.793 shareobjectdemo[4165:100897] I=9
2015-05-25 23:15:10.792 shareobjectdemo[4165:101000] 0x7face94950e0 excute2 i = 7 d=9
2015-05-25 23:15:10.793 shareobjectdemo[4165:100999] 0x7face9612c80 excute1 i = 9 d=9
2015-05-25 23:15:10.792 shareobjectdemo[4165:100998] 0x7face9614150 excute1 i = 8 d=8
2015-05-25 23:15:10.795 shareobjectdemo[4165:100999] 0x7face9612c80 excute2 i = 9 d=9
2015-05-25 23:15:10.795 shareobjectdemo[4165:100998] 0x7face9614150 excute2 i = 8 d=9
Analytical:
All say that dispatch_queue_create is creating a serial queue, so as long as the ID is the same, does the task must be serial?
Wrong.
2015-05-25 23:15:10.762 shareobjectdemo[4165:101000] 0x7face94950e0 excute1 i = 2 d=2
2015-05-25 23:15:10.764 shareobjectdemo[4165:100999] 0x7face9612c80 excute1 i = 4 d=4
These two lines can explain the problem. Excute1 i = 4 d=4 is not executed until excute1 i = 2 is executed (execution 0x7face94950e0 Excute2 i = 2 d=9).
and carefully observe the address of the loop, even if the ID is the same, it is not executed in the same thread. A mechanism that triggers the creation of a new child thread.
IOS Code Parsing