c:c_threads:create_a_thread:create_a_thread_with_a_class_object
This is an old revision of the document!
C - C++ Threads - Create a thread - Create a thread with a class object
Create a class object “Missile” and pass it to the thread constructor.
#include <thread> #include <iostream> using namespace std; // Create the function object. class Missile { public: void operator() () const { cout << "Firing Tomahawk missile" << endl; } }; int main() { // Create a thread with an function object. Missile tomahawk; thread t1(tomahawk); t1.join(); return 0; }
Using a class function
#include <thread> #include <iostream> using namespace std; class Torpedo { public: void LaunchTorpedo() { cout << " Launching Torpedo" << endl; } }; int main() { // Execute the LaunchTorpedo() method for a specific Torpedo object on a separate thread. Torpedo torpedo; thread t1(&Torpedo::LaunchTorpedo, &torpedo); t1.join(); return 0; }
c/c_threads/create_a_thread/create_a_thread_with_a_class_object.1614909769.txt.gz · Last modified: 2021/03/05 02:02 by peter