====== C - C++ Threads - Create a thread - Create a thread with a function pointer ======
Just pass in the address of a function to the thread constructor.
The thread will start executing the function immediately.
#include
#include
void ShowMessage()
{
std::cout << "Firing sidewinder missile " << std::endl;
}
int main()
{
// Create a thread with a function pointer.
std::thread t1(ShowMessage);
t1.join();
return 0;
}
----
===== With Arguments =====
#include
#include
#include
void ShowMessage(std::string name, int age)
{
std::cout << "Hello " << name << ". You are " << age << " years old." << std::endl;
}
int main()
{
std::thread t1(ShowMessage, "Peter", 21);
t1.join();
return 0;
}