fork(1) download
  1. #include <iostream>
  2. #include <future>
  3. #include <chrono>
  4. using namespace std;
  5. mutex outMutex;
  6. class Test
  7. {
  8. public:
  9. Test(){}
  10. void out(int No, double& q) const
  11. {
  12. {
  13. lock_guard<mutex> tst(outMutex);
  14. cout << "<- " << No << " -- " << &q << endl;
  15. }
  16. this_thread::sleep_for(chrono::seconds(1));
  17. }
  18. void test() const;
  19. };
  20. void Test::test() const
  21. {
  22. double w[2];
  23. cout << "-> " << 0 << " -- " << &w[0] << endl;
  24. future<void> f1 = async( &Test::out,this,0,ref(w[0]));
  25. {
  26. lock_guard<mutex> tst(outMutex);
  27. cout << "-> " << 1 << " -- " << &w[1] << endl;
  28. }
  29. future<void> f2 = async( &Test::out,this,1,ref(w[1]));
  30. f1.get();
  31. f2.get();
  32. }
  33. int main(int argc, const char * argv[])
  34. {
  35. try {
  36. Test t;
  37. t.test();
  38. }
  39. catch(exception&e)
  40. {
  41. cout << "\n" << e.what() << endl;
  42. }
  43. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
-> 0 -- 0xbfc076d0
-> 1 -- 0xbfc076d8
<- 0 -- 0xbfc076d0
<- 1 -- 0xbfc076d8