#include #include /* Program demonstrating multithreading using three different callables. */ using namespace std; // Calling a function void foo(int n) { for (int i = 0; i < n; i++) { cout << "Thread 1 using function pointer as callable\n"; } } // A callable object class thread_obj { public: void operator()(int n) { for (int i = 0; i < n; i++) cout << "Thread 2 using function object as callable\n"; } }; int main() { cout << "Threads 1 and 2 and 3 operating independently" << endl; // This thread is launched by using function pointer as callable thread th1(foo, 3); // This thread is launched by using function object as callable thread th2(thread_obj(), 3); // Define a Lambda Expression auto f = [](int n) { for (int i = 0; i < n; i++) cout << "Thread 3 using lambda expression as callable\n"; }; // This thread is launched by using lamda expression as callable thread th3(f, 3); // Wait for the threads to finish // Wait for thread t1 to finish th1.join(); // Wait for thread t2 to finish th2.join(); // Wait for thread t3 to finish th3.join(); cout << " Program terminates " << endl; return 0; }