队列barrier
2016-06-12 10:25
337 查看
NSMutableDictionary
- (void)testMutableDictionaryThreadSafe
{dispatch_semaphore_t sema = dispatch_semaphore_create(0);dispatch_async(concurrent_queue,
^{
for
(int index
=
0;
index<1000
;
index++) {dict[@(index)]
= @(index);
}
dispatch_async(concurrent_queue, ^{
for
(int index
=
0;
index<1000
;
index++) {
dict[@(index)]
= @(0);}
NSMutableDictionary
gcd
NSMutableDictionary
serial
queue + dispatch_async
dispatch_async
,
concurrent
queue + dispatch_async
dispatch_barrier_async
dispatch_barrier_async
dispatch_barrier_async
barrier
barrier
concurrent queue
typedef void
(^ThreadSafeDictionaryBlock)(ThreadSafeDictionary*dict,
NSString
*key,
id
object);
@interface
ThreadSafeDictionary
(){
- (id)init{
if
(self
= [super
init]) {
concurrentQueue = dispatch_queue_create("www.reviewcode
return self;}
- (void)objectForKey:(id)aKey
block:(ThreadSafeDictionaryBlock)block
{
id
key = [aKey
copy];
__weak
__typeof__(self)
weakSelf = self;dispatch_async(concurrentQueue,
^{
ThreadSafeDictionary *strongSelf = weakSelf;if
(!strongSelf)
return;
id
object = [self
objectForKey:key];block(self,
key, object);
});}
- (void)setObject:(id)object
forKey:(NSString
*)key block:(ThreadSafeDictionaryBlock)block
{
- (void)testMutableDictionaryThreadSafe
{dispatch_semaphore_t sema = dispatch_semaphore_create(0);dispatch_async(concurrent_queue,
^{
for
(int index
=
0;
index<1000
;
index++) {dict[@(index)]
= @(index);
}
dispatch_semaphore_signal(sema); });
dispatch_async(concurrent_queue, ^{
for
(int index
=
0;
index<1000
;
index++) {
dict[@(index)]
= @(0);}
dispatch_semaphore_signal(sema); });
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
NSLog(@"dict is %@", dict); }
NSMutableDictionary
gcd
NSMutableDictionary
serial
queue + dispatch_async
dispatch_async
,
concurrent
queue + dispatch_async
dispatch_barrier_async
dispatch_barrier_async
dispatch_barrier_async
barrier
barrier
concurrent queue
typedef void
(^ThreadSafeDictionaryBlock)(ThreadSafeDictionary*dict,
NSString
*key,
id
object);
@interface
ThreadSafeDictionary
(){
dispatch_queue_t concurrentQueue; }
@end @implementation ThreadSafeDictionary
- (id)init{
if
(self
= [super
init]) {
concurrentQueue = dispatch_queue_create("www.reviewcode
.cn", DISPATCH_QUEUE_CONCURRENT); }
return self;}
- (void)objectForKey:(id)aKey
block:(ThreadSafeDictionaryBlock)block
{
id
key = [aKey
copy];
__weak
__typeof__(self)
weakSelf = self;dispatch_async(concurrentQueue,
^{
ThreadSafeDictionary *strongSelf = weakSelf;if
(!strongSelf)
return;
id
object = [self
objectForKey:key];block(self,
key, object);
});}
- (void)setObject:(id)object
forKey:(NSString
*)key block:(ThreadSafeDictionaryBlock)block
{
相关文章推荐
- JAVA基础知识回顾-----File类-----实用
- COM原理与实现之一
- 数据挖掘-用户画像
- 由于使用了浮动使父元素高度不能自动撑起来
- a标签不能跳转,javascript控制其能跳转的方法
- COM原理与实现之一
- spark-sql
- tableView的局部刷新问题
- [转载]如何写测试计划
- minigui杂项
- Glide使用详解(一)
- Qt Qml 中Image的QSslSocket问题解决
- 分布式系统里session同步的那些事儿
- SecureCRT中中文出现乱码解决办法
- springmvc+mybatis+spring 整合
- Eclipse转战Android Studio心酸史
- KVC字典转模型以及常见错误解决(接上昨晚的自动生成属性)
- Android Manifest 权限描述大全
- 异或运算的运用
- java开发经典书籍