fork(2) download
  1. #include <iostream>
  2.  
  3. struct Settings
  4. {
  5. Settings(int v_) : v(v_) {}
  6. int v;
  7. };
  8.  
  9. //http://stackoverflow.com/a/11255852
  10. #define useHardwareWith(settings) \
  11. acquireHardware(settings); \
  12. for( \
  13.   struct \
  14.   { \
  15.   bool done() \
  16.   { \
  17.   return done_; \
  18.   } \
  19.   bool run() \
  20.   { \
  21.   releaseHardware(); \
  22.   done_ = true; \
  23.   } \
  24.   bool done_; \
  25.   } magic = {false}; !magic.done() ; magic.run())
  26.  
  27. void acquireHardware(const Settings& settings)
  28. {
  29. std::cout << "acquire(" << settings.v << ")\n";
  30. }
  31.  
  32. void releaseHardware()
  33. {
  34. std::cout << "release()\n";
  35. }
  36.  
  37. int main()
  38. {
  39. Settings settings(1);
  40. useHardwareWith(settings)
  41. {
  42. std::cout << "Doing important hardware stuff\n";
  43. }
  44. }
  45.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
acquire(1)
Doing important hardware stuff
release()