gpt4 book ai didi

objective-c - 为什么下面这个简单的 grand central dispatch 程序不能正常工作?

转载 作者:搜寻专家 更新时间:2023-10-30 19:56:13 26 4
gpt4 key购买 nike

所以我期待下面的程序打印两行。但是它不打印任何东西。关于需要解决的问题有什么想法吗?

#import <Foundation/Foundation.h>
#import <dispatch/dispatch.h>

int main(int argc, char **argv)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
printf("Done outer async\n");
dispatch_async(dispatch_get_main_queue(),^{
printf("Done inner sync");
});
});

return 0;
}

谢谢

最佳答案

如果您的程序没有事件循环,您必须调用 dispatch_main():

int main(int argc, char **argv)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
printf("Done outer async\n");
dispatch_async(dispatch_get_main_queue(),^{
printf("Done inner sync");
});
});

dispatch_main();
return 0;
}

来自文档:

This function "parks" the main thread and waits for blocks to be submitted to the main queue. Applications that call UIApplicationMain (iOS), NSApplicationMain (Mac OS X), or CFRunLoopRun on the main thread must not call dispatch_main.

关于objective-c - 为什么下面这个简单的 grand central dispatch 程序不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18549771/

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