std::this_thread::yield() vs std::this_thread::sleep_for()?

后端 未结 2 483
囚心锁ツ
囚心锁ツ 2020-12-13 06:06

I wanted to know what is the difference between C++11 std::this_thread::yield() and std::this_thread::sleep_for()? And how to decide what to use? T

2条回答
  •  离开以前
    2020-12-13 07:05

    std::this_thread::sleep_for()

    will make your thread sleep for a given time (the thread is stopped for a given time). (http://en.cppreference.com/w/cpp/thread/sleep_for)

    std::this_thread::yield()

    will stop the execution of the current thread and give priority to other process/threads (if there are other process/threads waiting in the queue). The execution of the thread is not stopped. (it just release the CPU). (http://en.cppreference.com/w/cpp/thread/yield)

提交回复
热议问题