Professional Documents
Culture Documents
CS609 Solution-2
CS609 Solution-2
PROGRAMMING
ASSIGNEMN-2
JUNAID MALIK
Display the message if thread is successfully created, otherwise display the error code using
GetLastError() function. Also, display the created thread ID in a main function. Inside the
“VUFunc”, display the string “Virtual University” ten times.
ANSWER:
#include <windows.h>
#include <iostream>
void VUFunc() {
for (int i = 0; i < 10; i++) {
std::cout << "Virtual University" << std::endl;
}
}
int main() {
DWORD threadId;
AL-JUNAID TECH INSTITUT
HANDLE hThread = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)VUFunc, NULL, 0, &threadId);
if (hThread == NULL) {
std::cout << "Error creating thread: " << GetLastError() << std::endl;
return 1;
}
std::cout << "Thread successfully created with ID: " << threadId <<
std::endl;
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
return 0;
}