sleep(), usleep()和[NSThread sleepForTimeInterval:]

本文解释了POSIX标准库中的sleep()和usleep()方法,以及Foundation框架中的NSThread.sleepForTimeInterval()方法之间的区别。它们都是通过调用nanosleep(2)系统调用来实现线程暂停的功能。

引用一段stackoverflow上的回答

sleep(3) is a posix standard library method that attempts to suspend the calling thread for the amount of time specified in seconds. usleep(3) does the same, except it takes a time in microseconds instead. Both are actually implemented with the nanosleep(2) system call.

The last method does the same thing except that it is part of the Foundation framework rather than being a C library call. It takes an NSTimeInterval that represents the amount of time to be slept as a double indicating seconds and fractions of a second.

For all intents and purposes, they all do functionally the same thing, i.e., attempt to suspend the calling thread for some specified amount of time.

原帖地址:

http://stackoverflow.com/questions/3155586/what-is-the-difference-among-sleep-usleep-nsthread-sleepfortimeinterval

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值