You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
1.9 KiB
85 lines
1.9 KiB
10 years ago
|
/*
|
||
9 years ago
|
* Copyright (c) 2014 - present Facebook, Inc.
|
||
|
* All rights reserved.
|
||
|
*
|
||
|
* This source code is licensed under the BSD style license found in the
|
||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||
|
*/
|
||
10 years ago
|
|
||
|
#import <Foundation/Foundation.h>
|
||
|
|
||
8 years ago
|
@interface DispatchEx : NSObject {
|
||
9 years ago
|
int x;
|
||
10 years ago
|
}
|
||
|
|
||
|
@end
|
||
|
|
||
8 years ago
|
@implementation DispatchEx
|
||
|
|
||
|
- init {
|
||
|
return self;
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
+ (int)dispatch_once_example {
|
||
8 years ago
|
static DispatchEx* a = nil;
|
||
9 years ago
|
// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
|
||
|
// 0),^{
|
||
|
static dispatch_once_t onceToken;
|
||
|
dispatch_once(&onceToken, ^{
|
||
8 years ago
|
a = [[DispatchEx alloc] init];
|
||
9 years ago
|
a->x = 10;
|
||
|
});
|
||
|
return a->x;
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
+ (int)dispatch_async_example {
|
||
8 years ago
|
static DispatchEx* a = nil;
|
||
9 years ago
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
|
||
|
^{
|
||
8 years ago
|
a = [[DispatchEx alloc] init];
|
||
9 years ago
|
a->x = 10;
|
||
|
});
|
||
|
return a->x;
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
+ (int)dispatch_after_example {
|
||
8 years ago
|
static DispatchEx* a = nil;
|
||
9 years ago
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),
|
||
|
dispatch_get_main_queue(),
|
||
|
^{
|
||
8 years ago
|
a = [[DispatchEx alloc] init];
|
||
9 years ago
|
a->x = 10;
|
||
|
});
|
||
|
return a->x;
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
+ (int)dispatch_group_example {
|
||
8 years ago
|
static DispatchEx* a = nil;
|
||
9 years ago
|
dispatch_group_async(NULL, dispatch_get_main_queue(), ^{
|
||
8 years ago
|
a = [[DispatchEx alloc] init];
|
||
9 years ago
|
a->x = 10;
|
||
|
});
|
||
|
return a->x;
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
+ (int)dispatch_group_notify_example {
|
||
8 years ago
|
static DispatchEx* a = nil;
|
||
9 years ago
|
dispatch_group_async(NULL, dispatch_get_main_queue(), ^{
|
||
8 years ago
|
a = [[DispatchEx alloc] init];
|
||
9 years ago
|
a->x = 10;
|
||
|
});
|
||
|
return a->x;
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
+ (int)dispatch_barrier_example {
|
||
8 years ago
|
static DispatchEx* a = nil;
|
||
9 years ago
|
dispatch_barrier_async(dispatch_get_main_queue(), ^{
|
||
8 years ago
|
a = [[DispatchEx alloc] init];
|
||
9 years ago
|
a->x = 10;
|
||
|
});
|
||
|
return a->x;
|
||
10 years ago
|
}
|
||
|
|
||
|
@end
|