Apple为什么弃用dispatch_get_current_queue?这次电话有什么不安全之处?
我很难找到关于如何使用这些功能的好例子.
static void * kQueue1Key = "key1";
static void * kQueue2Key = "key2";
dispatch_queue_t queue1 = dispatch_queue_create("com.company.queue1", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t queue2 = dispatch_queue_create("com.company.queue2", DISPATCH_QUEUE_SERIAL);
dispatch_queue_set_specific(queue1, kQueue1Key, (void *)kQueue1Key, NULL);
dispatch_queue_set_specific(queue2, kQueue2Key, (void *)kQueue2Key, NULL);
dispatch_sync(queue1, ^{
if(dispatch_get_specific(kQueue1Key))
{
NSLog(@"I'm expecting this line to run (A)");
dispatch_sync(queue2, ^{
NSLog(@"I'm expecting this line to run (B)");
if(dispatch_get_specific(kQueue2Key))
{
if(dispatch_get_specific(kQueue1Key))
{
NSLog(@"I'm expecting this line to run (C)");
}
else
{
[NSException raise:NSInternalInconsistencyException format:@"Should not end up here (C)"];
}
}
else
{
[NSException …Run Code Online (Sandbox Code Playgroud) 我刚读过这篇文章,其解决方案似乎令人信服:
我感兴趣的是,如果有可能推动这一方案来实现重入锁定机制并行调度队列(每次读取使用dispatch_sync做,写使用dispatch_barrier_async做,等进行了说明这里,看到"一个资源,多个读卡器,和一个单一的作家").
PS我想我已经设法在[NSThread currentThread].threadDictionary 这里实现了这个,但我不喜欢处理[NSThread currentThread]因为我依赖GCD.是否有可能[NSThread currentThread].threadDictionary用一些棘手的dispatch_set_specific/dispatch_get_specific代码替换使用?