有什么理由不在大中央调度队列中使用睡眠吗? [英] Is there any reason to not use sleep in a Grand Central Dispatch queue?

查看:92
本文介绍了有什么理由不在大中央调度队列中使用睡眠吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让队列在循​​环时等待一小段时间.我正在考虑我的选择,并且正在测试暂停恢复队列,但这似乎需要几个活动部分.因此,我正在考虑改为使用睡眠或非睡眠状态.那更多的是一般的线程功能,并且想知道我是否应该避免使用睡眠,而是坚持使用GCD选项来使队列暂停.

I would like to make a queue wait for a short period while it is looping. I am considering my options and was testing out suspending a resuming a queue but that seems to require several moving parts. So I am considering using sleep or usleep instead. That is more of a general threading function and would like to know if I should avoid using sleep and instead stick with GCD options to make a queue pause.

我找到了一个相关的问题,但该回答表明他只是缺少一个包含.是否有将睡眠呼叫与GCD队列混在一起的问题?

I found one related question but that answer shows that he was just missing an include. Are there any concerns with mixing sleep calls in with GCD queues?

iphone-在Grand Central Dispatch的辅助线程上可以使用usleep吗?

推荐答案

您可以使用sleep,但是正如您提到的,请不要在主线程上使用它,因为您永远都不应占用主线程.

You can use sleep, but as you mentioned, do it off of the main thread, as you should never tie up the main thread.

但是,如果您需要少量睡眠,则时间量可能不精确,也不太可能,但是线程至少会在睡眠量之后被唤醒,具体取决于其他可能使用的时间. cpu.

But, if you need a small sleep, the amount of time may not be precise, and is unlikely to be, but the thread will be woken up at least after the amount of sleep, depending on what else may be using the cpu.

但是,我认为使用睡眠没有问题,因为它至少会使其他线程/应用程序有运行的机会.

But, I see no problem with using sleep as it will at least give other threads/applications, a chance to run.

这篇关于有什么理由不在大中央调度队列中使用睡眠吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆