실제로 실행하려는 코드와 실행 방법에 따라 달라집니다. 이렇게하는 간단한 방법은 별도의 스레드를 만들고이 스레드에 Sleep()
을 작성하는 것입니다. Visual Studio 2008에서 업그레이드 할 수 없으므로 (정확히 기억한다면 C++ 11을 지원하지 않습니다), 기본 Windows 스레드 또는 Boost.Thread와 같은 라이브러리 구현을 사용해야합니다. Windows 스레드 사용 방법을 찾으려면 MSDN documentation on _beginthreadex() function을 참조하십시오. Boost.Thread에 대한 간단한 자습서는 here으로 볼 수 있습니다.
1) 윈도우 스레드 : 두 번째 예에서
struct callable
{
void operator()();
};
boost::thread copies_are_safe()
{
callable x;
return boost::thread(x);
} // x is destroyed, but the newly-created thread has a copy, so this is OK
:
// crt_begthrdex.cpp
// compile with: /MT
#include <windows.h>
#include <stdio.h>
#include <process.h>
unsigned Counter;
unsigned __stdcall SecondThreadFunc(void* pArguments)
{
printf("In second thread...\n");
while (Counter < 1000000)
Counter++;
_endthreadex(0);
return 0;
}
int main()
{
HANDLE hThread;
unsigned threadID;
printf("Creating second thread...\n");
// Create the second thread.
hThread = (HANDLE)_beginthreadex(NULL, 0, &SecondThreadFunc, NULL, 0, &threadID);
// Wait until second thread terminates. If you comment out the line
// below, Counter will not be correct because the thread has not
// terminated, and Counter most likely has not been incremented to
// 1000000 yet.
WaitForSingleObject(hThread, INFINITE);
printf("Counter should be 1000000; it is-> %d\n", Counter);
// Destroy the thread object.
CloseHandle(hThread);
}
2) Boost.Thread의는 I가 제공하는 링크에서 직접 촬영 모두의
빠른 예, , 일반 함수 포인터를 boost::thread
생성자 인수로 사용할 수도 있습니다. 또한, 포인터를 사용하여 다중 인수로 함수를 사용할 수 있습니다. 고급 Windows API의 스레드는 제공하지 않습니다.
이 오류가 발생했습니다 오류 LNK2019 : 확인되지 않은 외부 기호 __imp__timeGetTime @ 0이 (가) _main 함수에서 참조되었습니다. u 헤더 파일을 놓쳤습니까? – Mark
보고있는 것은 링커 오류입니다.timeGetTime()은 Windows 멀티미디어 라이브러리의 일부이므로 winmm.lib를 라이브러리 목록에 추가하여 해당 라이브러리 (Windows 2000부터 모든 Windows 버전에 표준으로 포함되어 있음)에 링크해야합니다. 프로그램. –
또는 파일의 맨 위에 다음 행을 추가 할 수 있습니다. #pragma comment (lib, "winmm.lib") –