fork(1) download
  1. #include <iostream>
  2. #include <boost/bind.hpp>
  3. using namespace std;
  4.  
  5. template<class function_type>
  6. void retry(function_type function)
  7. {
  8. const int numOfRetries = 3;
  9. int i = 1;
  10. do
  11. {
  12. //Invoke the function that was passed as argument
  13. if(function())
  14. {
  15. //Invocation is successful
  16. cout << "\t try number#" << i <<" Successful \n";
  17. break;
  18. }
  19.  
  20. //Invocation is Not successful
  21. cout << "\t try number#" << i <<" Not Successful \n";
  22. ++i;
  23.  
  24. if (i == 4)
  25. {
  26. cout<< "\t failed invocation!";
  27. }
  28.  
  29. }while (i <= numOfRetries);
  30. }
  31.  
  32. int Permit(int i)
  33. {
  34. //Permit succeeds the second retry
  35. static int x = 0;
  36. x++;
  37. if (x == 2 && i ==1 ) return 1;
  38. else return 0;
  39. }
  40.  
  41. int main()
  42. {
  43. int i = 1;
  44. retry(boost::bind(Permit, i));
  45. }
Success #stdin #stdout 0.02s 2724KB
stdin
Standard input is empty
stdout
	 try number#1 Not Successful 
	 try number#2 Successful