Can we run two functions parallel in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can we run two functions parallel in C++?

21st May 2017, 1:32 PM
Manish Kumar
Manish Kumar - avatar
4 Answers
0
If you create a separate thread, yes.
21st May 2017, 1:54 PM
aklex
aklex - avatar
0
Sure, just a sec.
21st May 2017, 2:00 PM
aklex
aklex - avatar
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
21st May 2017, 2:14 PM
aklex
aklex - avatar
- 1
Can you give any example?
21st May 2017, 1:59 PM
Manish Kumar
Manish Kumar - avatar