#include #include #include #include using namespace std ; void f1(int n) { for (int i = 0; i < 5; ++i) { cout << "Thread f1 executing\n"; ++n; this_thread::sleep_for(chrono::milliseconds(10)); } } void f2(int& n2) { for (int i1 = 0; i1 < 5; ++i1) { cout << "Thread f2 executing: " << i1 << "\n"; ++n2; this_thread::sleep_for(chrono::milliseconds(10)); } n2 = 101 ; } class foo { public: void bar() { for (int i = 0; i < 5; ++i) { cout << "Thread bar executing\n"; ++n; this_thread::sleep_for(chrono::milliseconds(10)); } } int n = 0; }; class baz { public: void operator()() { for (int i = 0; i < 5; ++i) { cout << "Thread baz executing\n"; ++n; this_thread::sleep_for(chrono::milliseconds(10)); } } int n = 0; }; int main() { int n1 = 0; foo f; baz b; thread t1; // t1 does not run anything thread t2(f1, n1 + 1); // pass by value //Thread 2 thread t3(f2, ref(n1) ); // pass by reference this_thread::sleep_for(chrono::milliseconds(1)); cout << "In main about to shift t3 to t4:" << endl ; thread t4( move(t3) ); // t4 is now running f2(). t3 is no longer a thread thread t5(&foo::bar, &f); //executing a method in a class thread t6(b); //Executing a functor t2.join(); t4.join(); t5.join(); t6.join(); cout << "The value of n1: " << n1 << endl ; }