fork(2) download
  1. #include <vector>
  2. #include <mutex>
  3. using namespace std;
  4.  
  5. template<typename T>
  6. class monitor
  7. {
  8. mutable mutex m;
  9. mutable T t;
  10. public:
  11. typedef T &ref;
  12. template<typename F>
  13. auto operator*(F f) const -> decltype(f(t))
  14. {
  15. return lock_guard<mutex>(m),
  16. f(t);
  17. }
  18. };
  19. #define MONITOR(mon, z) mon * [&](decltype(mon)::ref z)
  20.  
  21. int main()
  22. {
  23. monitor<vector<int>> m;
  24. MONITOR(m, x)
  25. {
  26. x.resize(10);
  27. x.push_back(1);
  28. };
  29. }
Success #stdin #stdout 0s 3424KB
stdin
Standard input is empty
stdout
Standard output is empty