fork download
  1. #include <iostream>
  2. #include <chrono>
  3. #include <thread>
  4. #include <mutex>
  5. #include <cmath>
  6. using namespace std;
  7. mutex g_lock;
  8. int n;
  9. void first(){
  10.  
  11. g_lock.lock();
  12. cout << "Enter the number of terms of Fibonacci series you want" << endl;
  13. cin >> n;
  14. g_lock.unlock();
  15.  
  16. }
  17. void second(){
  18. int c,first = 0, second = 1, next;
  19.  
  20. g_lock.lock();
  21.  
  22. for ( c = 0 ; c < n ; c++ )
  23. {
  24. if ( c <= 1 )
  25. next = c;
  26. else
  27. {
  28. next = first + second;
  29. first = second;
  30. second = next;
  31. }
  32. cout << next << endl;
  33. }
  34. g_lock.unlock();
  35.  
  36.  
  37. }
  38. int main()
  39. {
  40.  
  41.  
  42. std::thread thr2(second);
  43. std::thread thr1(first);
  44.  
  45. thr1.detach();
  46. thr2.join();
  47.  
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0s 19856KB
stdin
Standard input is empty
stdout
Enter the number of terms of Fibonacci series you want