InterviewSolution
| 1. |
How Can A C++11 Thread Be Created With A Function Object? |
|
Answer» CREATE a function OBJECT "Missile" and pass it to the thread constructor. #include "stdafx.h" #include <thread> #include <iostream> using NAMESPACE std; //Create the function object class Missile { public: void operator() () const { COUT << "Firing Tomahawk missile" << endl; } }; int main() { //Creating a thread with an function object Missile tomahawk; thread t1(tomahawk); t1.join(); return 0; } Create a function object "Missile" and pass it to the thread constructor. #include "stdafx.h" #include <thread> #include <iostream> using namespace std; //Create the function object class Missile { public: void operator() () const { cout << "Firing Tomahawk missile" << endl; } }; int main() { //Creating a thread with an function object Missile tomahawk; thread t1(tomahawk); t1.join(); return 0; } |
|