Was going through some tutorials on multi-threading and wrote some code to create an infinite thread. However, the infinite loop doesn't seem to work. The print routine inside the loop just runs once. Find the code below for reference. Cant understand what I am doing wrong. I am using the MSVC compiler.
#include <Windows.h>
#include <iostream>
DWORD __stdcall ThreadProc(LPVOID lpParameter) {
const char* StringToPrint = (const char*)lpParameter;
//expected this block to run infinitely.
while (1) {
std::cout << StringToPrint << "\n";
Sleep(1000);
}
return 0;
}
int main() {
DWORD ThreadId = 0;
const char *Param = "Inside Thread";
LPVOID Param1 = (LPVOID)Param;
HANDLE Threadhandle = CreateThread(0, 0, ThreadProc, Param1, 0, &ThreadId);
std::cout << "Thread ID " << ThreadId << " started" << "\n";
CloseHandle(Threadhandle);
return 0;
}