c:c_threads:create_a_thread:create_a_thread_with_a_function_pointer
This is an old revision of the document!
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 <thread> #include <iostream> using namespace std; void FireMissile() { cout << "Firing sidewinder missile " << endl; } int main() { // Create a thread with a function pointer. thread t1(FireMissile); t1.join(); return 0; }
With Arguments
#include <string> #include <thread> #include <iostream> using namespace std; void FireTorpedo(int numCities, string torpedoType) { cout << "Firing torpedo " << torpedoType << " at" << numCities << " cities." << endl; } int main() { thread t1(FireTorpedo, 3, "HungryShark"); t1.join(); return 0; }
c/c_threads/create_a_thread/create_a_thread_with_a_function_pointer.1614910262.txt.gz · Last modified: 2021/03/05 02:11 by peter