this_thread::sleep_for(0) 와 this_thread::yield() 의 차이점
C++/C++ 2021. 6. 27. 00:20 |C++에서 this_thread::yield()의 동작
정확한 동작은 구현( 사용 중인 OS 스케줄러의 메커니즘과 시스템 상태)에 따라 달라진다.
예를 들어, 선입선출 실시간 스케줄러(Linux의 경우 SCHED_FIFO)는
현재 스레드를 일시 중단하여 실행할 준비가 된 동일한 우선 순위 스레드의 대기열 뒤에 배치한다
(동일한 우선 순위의 다른 스레드가 없으면 yield 는 별다른 동작을 하지 않음).
참고 - https://en.cppreference.com/w/cpp/thread/yield
std::this_thread::yield - cppreference.com
void yield() noexcept; (since C++11) Provides a hint to the implementation to reschedule the execution of threads, allowing other threads to run. [edit] Parameters (none) [edit] Return value (none) The exact behavior of this function depends on the impleme
en.cppreference.com
C#에서 Thread.Sleep(0) 과 Thread.Yield() 의 차이점
윈도우 운영체제에서의 차이점 by Microsoft
Thread.yield() [.NET 4 Beta 1]: yields to any thread on same processor
- Advantage : about twice as fast as Thread.Sleep(0)
- Disadvantage : yields only to threads on same processor
Thread.sleep(0) : yields to any thread of same or higher priority on any processor
- Advantage : faster than Thread.Sleep(1)
- Disadvantage : yields only to threads of same or higher priority
Thread.Sleep(1) : yields to any thread on any processor
- Advantage : yields to any thread on any processor
- Disadvantage : slowest option
(Thread.Sleep(1) will usually suspend the thread by about 15ms
if timeBeginPeriod/timeEndPeriod [win32] are not used)
Yield()는 현재 thread를 실행 중인 CPU 코어에서 실행되는 스레드한테 CPU 점유권을 넘긴다.
이때 우선순위에 상관없이 아무 스레드 한테 CPU 점유를 넘기기 때문에 Starvation이 덜 일어난다.
또한 time-slice를 넘겨받은 thread의 존재 여부를 boolean 값으로 반환한다.
sleep(0)은 우선 순위가 현재 스레드보다 낮은 스레드에게 time-slice가 넘어가는 것을 방지하기 때문에 Starvation이 발생하기 쉽다.
이는 마이크로소프트사의 윈도우 OS 기반으로 한 설명이므로, 다른 OS를 사용한다면 다를 수 있음에 주의.
'C++ > C++' 카테고리의 다른 글
Effective C++ 컴파일러가 만든 함수가 필요 없는 경우 이를 제거하라 (0) | 2021.06.27 |
---|---|
Effective C++ 상속된 클래스가 있는 경우 가상 소멸자를 사용 (0) | 2021.06.27 |
생성자와 관련된 내용들 정리 (0) | 2021.06.15 |
C++ 접근 지정자(Access specifiers) (0) | 2021.06.13 |
Variable-Length Array 배열 매개변수로 넘기는 쉬운 방법 (0) | 2021.06.12 |