1. most of the time, we need to ensure the security of read and write data. At this time, it is best not to use @ synchronized synchronization block, because the code in the synchronization block must be executed separately, this may cause the execution of the current Code to continue after many irrelevant code is executed, reducing the program running efficiency. There are also nslock and nsrecursivelock locks, but these locks should also be used less,
The best solution is to use the GCD queue to ensure data security. Based on the Xun kernel, gcd provides many underlying optimizations.
dispatch_queue_t serialQ = dispatch_queue_create("testGCDQueue",DISPATCH_QUEUE_SERIAL); dispatch_barrier_async(serialQ, ^{ // safe });
dispatch_queue_t serialQ = dispatch_queue_create("testGCDQueue",DISPATCH_QUEUE_SERIAL); dispatch_barrier_sync(serialQ, ^{ // safe });
If there is a small amount of code in the block, you will find that Asyn is slower than SYN because Asyn needs to copy the block. However, when the logical operation in the block is responsible, you should check the specific situation.
Data security is generally ensured in the set and get methods. Therefore, data can be read and written in multiple read/write modes.
If you use a concurrent queue, you can use SYN in the set to use Asyn in the get, but the read/write operations may be performed in parallel, so this is not very safe.
A fence block is provided in GCD, which will be executed after all the previous Task blocks are executed, and the blocks after the fence block need to be executed after the fence blocks are executed, this improves security and concurrent execution.
dispatch_barrier_async(cerialQ, ^{ // safe });
2. The performselector series methods have some omissions in memory management. He cannot determine the specific selection to be executed, so the arc compiler cannot insert appropriate memory management methods.
In addition, per has many limitations. All the functions that can be implemented by per can be implemented by GCD or operation.
3. The dipatch_group mechanism can group the queue, and the caller can know when the execution of this group of tasks is completed.
dispatch_group_t group = dispatch_group_create();long b = dispatch_group_wait(group, DISPATCH_TIME_FOREVER); NSLog(@"111111Hello, World!%ld",b); // NSLog(@"Hello, World!%ld",a); dispatch_sync(queue1, ^{ for (int i = 0 ; i< 10000 ;i++) { NSLog(@"cccccccc"); } NSLog(@"ququeee1"); }); NSLog(@"3333333Hello, World!%ld",b);
The above Code creates a dispatch_group and waits for queue1 to run. dispatch_time_forever is always waiting for execution. If it is always waiting, the wait function returns 0 ., if it is not dispatch_time_forever, but a number, if the number is smaller than the execution time, the return value is not 0. otherwise, 0 is returned.