相关疑难解决方法(0)

Apple为什么弃用dispatch_get_current_queue?

Apple为什么弃用dispatch_get_current_queue?这次电话有什么不安全之处?

objective-c grand-central-dispatch

9
推荐指数
1
解决办法
5064
查看次数

如何使用dispatch_queue_set_specific()和dispatch_get_specific()

我很难找到关于如何使用这些功能的好例子.

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)

grand-central-dispatch objective-c-blocks

7
推荐指数
1
解决办法
5266
查看次数

如何通过调度并发队列(GCD)实现可重入锁定机制?

我刚读过这篇文章,其解决方案似乎令人信服:

  • 串行队列用于同步访问
  • dispatch_get_specific/dispatch_set_specific用于提供重入功能.

我感兴趣的是,如果有可能推动这一方案来实现重入锁定机制并行调度队列(每次读取使用dispatch_sync做,写使用dispatch_barrier_async做,等进行了说明这里,看到"一个资源,多个读卡器,和一个单一的作家").

PS我想我已经设法在[NSThread currentThread].threadDictionary 这里实现了这个,但我不喜欢处理[NSThread currentThread]因为我依赖GCD.是否有可能[NSThread currentThread].threadDictionary用一些棘手的dispatch_set_specific/dispatch_get_specific代码替换使用?

objective-c grand-central-dispatch

5
推荐指数
1
解决办法
985
查看次数