gpt4 book ai didi

objective-c-blocks - 如何使用 dispatch_queue_set_specific() 和 dispatch_get_specific()

转载 作者:行者123 更新时间:2023-12-04 10:04:23 25 4
gpt4 key购买 nike

我很难找到关于如何使用这些函数的好例子。

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 raise:NSInternalInconsistencyException format:@"Should not end up here (B)"];
}
});
}
else
{
[NSException raise:NSInternalInconsistencyException format:@"Should not end up here (A)"];
}
});

结果
I'm expecting this line to run (A)
I'm expecting this line to run (B)
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Should not end up here (C)'

这是预期的行为吗?如果我要 dispatch_sync 到 queue1 因为我不在队列中,我会死锁。我错过了什么?

最佳答案

哦,在这里,它突然出现在我的脑海中,为什么你得到了你得到的。注意事项:

dispatch_sync(queue1, ^{

当你到达这一点时,“当前队列”是 queue1
    if(dispatch_get_specific(kQueue1Key))

您正在向当前队列询问它对 kQueue1Key 的值,你早点设置,所以它把它还给你。
    {
NSLog(@"I'm expecting this line to run (A)");

dispatch_sync(queue2, ^{

当你到达这一点时,“当前队列”现在是 queue2
            NSLog(@"I'm expecting this line to run (B)");

if(dispatch_get_specific(kQueue2Key))

您正在向当前队列询问它对 kQueue2Key 的值,你早点设置,所以它把它还给你。
            {

if(dispatch_get_specific(kQueue1Key))

您现在正在向当前队列询问它对 kQueue1Key 的值。 .由于当前队列是 queue2并且您从不使用 kQueue1Key 设置值在 queue2你回来 NULL .
                {
NSLog(@"I'm expecting this line to run (C)");
}
else
{
[NSException raise:NSInternalInconsistencyException format:@"Should not end up here (C)"];
}

这里的误解是 dispatch_get_specific不遍历嵌套队列的堆栈,它遍历目标沿袭的队列。例如,如果你这样做,
static void * kQueue1Key = (void*)"key1";
static void * kQueue2Key = (void*)"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);

// Set Queue2 to target Queue1
dispatch_set_target_queue(queue2, queue1);

dispatch_sync(queue2, ^{

if(dispatch_get_specific(kQueue1Key))
{
NSLog(@"I'm expecting this line to run (A)");
}
else
{
[NSException raise:NSInternalInconsistencyException format:@"Should not end up here (C)"];
}

if(dispatch_get_specific(kQueue2Key))
{
NSLog(@"I'm expecting this line to run (B)");
}
else
{
[NSException raise:NSInternalInconsistencyException format:@"Should not end up here (C)"];
}
});

...定位关系是被遍历的关系,而不是堆栈关系。如果有什么东西可以遍历堆栈关系就好了,但我什么都不知道(您不必自己实现)。

关于objective-c-blocks - 如何使用 dispatch_queue_set_specific() 和 dispatch_get_specific(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19833744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com