Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. template<typename T> struct State
  2. {
  3. std::map<std::string, T> statedata;
  4. static std::mutex mux_state;
  5. ...
  6. void update(std::string k, T t)
  7. {
  8. while (!mux_state.try_lock()) timeoutdelay(TOD_UPDATE);
  9. ..do update..
  10. mux_state.unlock();
  11. }
  12. ...
  13. T read(std::string k)
  14. {
  15. while (!mux_state.try_lock()) timeoutdelay(TOD_READ);
  16. T t=statedata[k];
  17. mux_state.unlock();
  18. return t;
  19. }
  20. ...
  21. }
  22. ...
  23. State<mytype> state;
  24. ...
  25. ..from thread_1() randomly
  26. state.update(..)
  27. ...
  28. ..from thread_2() randomly
  29. state.read(..)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement