0
Can we run two functions parallel in C++?
4 Answers
0
If you create a separate thread, yes.
0
Sure, just a sec.
0
#include <iostream>
#include <Windows.h>
#include <string>
void ourFunc(const std::string& msg)
{
while (true)
{
std::cout << msg + "\n";
Sleep(5);
}
}
int main()
{
std::string msg = "I am running in a new thread!";
HANDLE threadHandle = CreateThread(nullptr, NULL, reinterpret_cast<LPTHREAD_START_ROUTINE>(ourFunc),
reinterpret_cast<LPVOID>(&msg), NULL, nullptr);
//do some other stuff while ourFunc is running in a different thread.
}
If you want some documentation on CreateThread, go here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682453(v=vs.85).aspx
- 1
Can you give any example?