search

Home  >  Q&A  >  body text

iOS里的sleep函数会阻塞什么

偶然发现Objective-C里的 sleep 函数,是一个宏定义,如下

unsigned int
	 sleep(unsigned int) __DARWIN_ALIAS_C(sleep);

那么这个sleep会阻塞哪些东西呢?是不是所有的Operation都会被阻塞?还是只有主线程被阻塞?
通过 detachNewThreadSelector: toTarget: withObject: 方法出来的线程会不会受影响?
另外,如果我连续运行两次:

sleep(1);
sleep(1);

这样,第二个sleep是从第一个sleep的1s结束后才开始计算么?

伊谢尔伦伊谢尔伦2772 days ago988

reply all(2)I'll reply

  • 阿神

    阿神2017-04-17 11:26:01

    Block the current thread. Once this sentence is seen, the current thread hangs for a given time and then executes the next sentence.

    reply
    0
  • 黄舟

    黄舟2017-04-17 11:26:01

    This sleep() function should only sleep the thread currently executing this method

    sleep(1);
    sleep(1);

    Looking at these two lines of code alone, they are just synchronization methods. The second line must be executed only after the first line is executed

    reply
    0
  • Cancelreply