iOS multi-threading of the GCD (Grand Central dispatch) (b)

This article then went on a talk. Major topics: dispatch_source.

dispatch_source main user to monitor the event, you can listen for the following events

DISPATCH_SOURCE_TYPE_DATA_ADD
DISPATCH_SOURCE_TYPE_DATA_OR
DISPATCH_SOURCE_TYPE_MACH_RECV
DISPATCH_SOURCE_TYPE_MACH_SEND
DISPATCH_SOURCE_TYPE_PROC
DISPATCH_SOURCE_TYPE_READ
DISPATCH_SOURCE_TYPE_SIGNAL
DISPATCH_SOURCE_TYPE_TIMER
DISPATCH_SOURCE_TYPE_VNODE
DISPATCH_SOURCE_TYPE_WRITE
DISPATCH_SOURCE_TYPE_MEMORYPRESSURE

More common is the realization Timer and custom events (DISPATCH_SOURCE_TYPE_DATA_ADD, DISPATCH_SOURCE_TYPE_DATA_OR).

First, to achieve timer

NSTimer will use a circular reference problem, use dispatch_source there would be no problem, as follows

__block NSInteger count = 0;
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 2.f * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        NSLog(@"count == %li", count++);
    });
    dispatch_resume(timer);

Note that the automatic execution after dispatch_source_t not be created, to be displayed to call dispatch_resume will be implemented, and will be performed once immediately after the call to resume.

Second, monitor user-defined events

DISPATCH_SOURCE_TYPE_DATA_ADD dispatch_source_merge_data will accumulate data submitted, DISPATCH_SOURCE_TYPE_DATA_OR data is submitted or operation. The following sample code

@interface ViewController10 ()

@property (nonatomic, strong) dispatch_source_t source;

@end

@implementation ViewController10

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog ( @ " dispatch_source_t implement custom event " );
     // simulator will be the first performance Caton, real machine does not, I do not know why 
    __block NSInteger Total = 0 ;
    dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_DATA_ADD, 0, 0, dispatch_queue_create("com.wk.serialQueue", DISPATCH_QUEUE_CONCURRENT));
    dispatch_source_set_event_handler(source, ^{
        NSInteger result = dispatch_source_get_data(source);
        total += result;
        NSLog(@"total = %li", total);
    });
    
    dispatch_source_set_cancel_handler(source, ^{
        NSLog ( @ " canceled the distributed source " );
    });
    
    dispatch_resume(source);
    
    self.source = source;
    
    dispatch_queue_t global = dispatch_get_global_queue(QOS_CLASS_UTILITY, 0);
    
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(10);
    
    
    for (int i = 0; i < 100; i++) {
        dispatch_async(global, ^{
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            
            dispatch_source_merge_data(source, 1);
            
            sleep(1.f);
            
            dispatch_semaphore_signal(semaphore);
        });
    }
}

- (void)dealloc
{
    dispatch_source_cancel(self.source);
}
dispatch_source_cancel can cancel distribute the source, the distribution of staff is not listening on the specified event 
you can download demo

Reproduced in: https: //www.cnblogs.com/pretty-guy/p/8241178.html

Guess you like

Origin blog.csdn.net/weixin_34217773/article/details/93199971