1.//Create Global queue
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
Create semaphore
dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
nsmutablearray *array = [nsmutablearray array];
for(int i = 0 ; i < 1000000; i + +) {
dispatch_async(queue, ^{
Dispatch_semaphore Semaphore is a multi-threaded synchronization mechanism based on counter. When multiple threads access a common resource, there is a problem of data error caused by the feature of multithreading, which causes it to wait for the previous thread to access the array
dispatch_semaphore_wait(semaphore, dispatch_time_forever);
[array addobject: [nsnumber numberwithint: i]];
});
}
2.
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
nsmutablearray *array = [nsmutablearray array];
for(int i = 0 ; i < 1000000; i + +) {
dispatch_async(queue, ^{
Adds a lock to the array object. Prevents thread access at the same time.
@synchronized (array) {
[Array addobject: [nsnumber numberwithint: i]];
NSLog(@ "%d%@", I,array);
}
});
}
Application of semaphore and @synchronized of signal volume