밀리 초 동안 절전 모드
POSIX sleep(x)
기능이 프로그램을 x 초 동안 잠자 게 만든다는 것을 알고 있습니다. C ++에서 x 밀리 초 동안 프로그램을 수면 상태로 만드는 기능이 있습니까?
밀리 초에 대한 표준 C API가 없으므로 (Unix에서) usleep
마이크로 초를 허용하는 을 설정해야합니다 .
#include <unistd.h>
unsigned int microseconds;
...
usleep(microseconds);
C ++ 11에서는 표준 라이브러리 기능으로이를 수행 할 수 있습니다.
#include <chrono>
#include <thread>
std::this_thread::sleep_for(std::chrono::milliseconds(x));
명확하고 읽기 쉬우므로 더 이상 sleep()
함수가 사용 하는 단위를 추측 할 필요가 없습니다 .
휴대 성을 유지하려면 수면을 위해 Boost :: Thread 를 사용할 수 있습니다 .
#include <boost/thread/thread.hpp>
int main()
{
//waits 2 seconds
boost::this_thread::sleep( boost::posix_time::seconds(1) );
boost::this_thread::sleep( boost::posix_time::milliseconds(1000) );
return 0;
}
이 답변은 중복되며 이전 에이 질문 에 게시되었습니다 . 아마도 거기에서도 유용한 답변을 찾을 수있을 것입니다.
Unix에서는 usleep 을 사용할 수 있습니다 .
Windows에는 Sleep이 있습니다.
플랫폼에 따라 가지고 usleep
있거나 nanosleep
사용할 수 있습니다. usleep
더 이상 사용되지 않으며 최신 POSIX 표준에서 삭제되었습니다. nanosleep
선호됩니다.
time.h 라이브러리를 사용하지 않는 이유는 무엇입니까? Windows 및 POSIX 시스템에서 실행 :
#include <iostream>
#include <time.h>
using namespace std;
void sleepcp(int milliseconds);
void sleepcp(int milliseconds) // Cross-platform sleep function
{
clock_t time_end;
time_end = clock() + milliseconds * CLOCKS_PER_SEC/1000;
while (clock() < time_end)
{
}
}
int main()
{
cout << "Hi! At the count to 3, I'll die! :)" << endl;
sleepcp(3000);
cout << "urrrrggghhhh!" << endl;
}
수정 된 코드-이제 CPU가 IDLE 상태로 유지됨 [2014.05.24] :
#include <iostream>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif // _WIN32
using namespace std;
void sleepcp(int milliseconds);
void sleepcp(int milliseconds) // Cross-platform sleep function
{
#ifdef _WIN32
Sleep(milliseconds);
#else
usleep(milliseconds * 1000);
#endif // _WIN32
}
int main()
{
cout << "Hi! At the count to 3, I'll die! :)" << endl;
sleepcp(3000);
cout << "urrrrggghhhh!" << endl;
}
nanosleep
보다 나은 선택입니다 usleep
-인터럽트에 대해 더 탄력적입니다.
#include <windows.h>
통사론:
Sleep ( __in DWORD dwMilliseconds );
용법:
Sleep (1000); //Sleeps for 1000 ms or 1 sec
MS Visual C ++ 10.0을 사용하는 경우 표준 라이브러리 기능을 사용하여이를 수행 할 수 있습니다.
Concurrency::wait(milliseconds);
필요할 것이예요:
#include <concrt.h>
C ++에서 프로그램을 잠자는 Sleep(int)
방법 은 방법입니다. 그것에 대한 헤더 파일은#include "windows.h."
예를 들면 :
#include "stdafx.h"
#include "windows.h"
#include "iostream"
using namespace std;
int main()
{
int x = 6000;
Sleep(x);
cout << "6 seconds have passed" << endl;
return 0;
}
휴면 시간은 밀리 초 단위로 측정되며 제한이 없습니다.
Second = 1000 milliseconds
Minute = 60000 milliseconds
Hour = 3600000 milliseconds
select
기능 이있는 플랫폼 (POSIX, Linux 및 Windows)에서 다음을 수행 할 수 있습니다.
void sleep(unsigned long msec) {
timeval delay = {msec / 1000, msec % 1000 * 1000};
int rc = ::select(0, NULL, NULL, NULL, &delay);
if(-1 == rc) {
// Handle signals by continuing to sleep or return immediately.
}
}
However, there are better alternatives available nowadays.
Select call is a way of having more precision (sleep time can be specified in nanoseconds).
Use Boost asynchronous input/output threads, sleep for x milliseconds;
#include <boost/thread.hpp>
#include <boost/asio.hpp>
boost::thread::sleep(boost::get_system_time() + boost::posix_time::millisec(1000));
The question is old, but I managed to figure out a simple way to have this in my app. You can create a C/C++ macro as shown below use it:
#ifndef MACROS_H
#define MACROS_H
#include <unistd.h>
#define msleep(X) usleep(X * 1000)
#endif // MACROS_H
As a Win32 replacement for POSIX systems:
void Sleep(unsigned int milliseconds) {
usleep(milliseconds * 1000);
}
while (1) {
printf(".");
Sleep((unsigned int)(1000.0f/20.0f)); // 20 fps
}
참고URL : https://stackoverflow.com/questions/4184468/sleep-for-milliseconds
'your programing' 카테고리의 다른 글
디버그 빌드 전용 Visual Studio 빌드 후 이벤트를 실행하는 방법 (0) | 2020.10.03 |
---|---|
Notepad ++에서 중복 행 제거 (0) | 2020.10.03 |
functools.wraps는 무엇을합니까? (0) | 2020.10.03 |
잘못된 Git 브랜치에 대한 커밋을 수정하는 방법은 무엇입니까? (0) | 2020.10.03 |
Vim에서 수직 분할에서 수평 분할로 빠르게 전환하려면 (0) | 2020.10.03 |