/* * c++thread1.c++ basic c++ thread * $ g++ -Wall -std=c++11 c++thread1.c++ -pthread */ #include #include using namespace std; #include int fact; void factorial(int n){ fact=1; for(int i=1; i<=n; ++i) fact *= i; sleep(1); cout << "child thread ID: " << this_thread::get_id() << endl; } int main(){ int n; cout << "Enter a +ve integer: "; cin >> n; std::thread t(factorial, n); cout << "main thread ID: " << this_thread::get_id() << endl; cout << "child thread ID (in parent): " << t.get_id() << endl; t.join(); cout << n << "! = " << fact << endl; return 0; }