CondSh.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #pragma once
  2. /*
  3. * CondSh.h, part of VCMI engine
  4. *
  5. * Authors: listed in file AUTHORS in main folder
  6. *
  7. * License: GNU General Public License v2.0 or later
  8. * Full text of license available in license.txt file, in main folder
  9. *
  10. */
  11. /// Used for multithreading, wraps boost functions
  12. template <typename T> struct CondSh
  13. {
  14. T data;
  15. boost::condition_variable cond;
  16. boost::mutex mx;
  17. CondSh()
  18. {}
  19. CondSh(T t)
  20. {
  21. data = t;
  22. }
  23. void set(T t)
  24. {
  25. boost::unique_lock<boost::mutex> lock(mx);
  26. data=t;
  27. }
  28. void setn(T t) //set data and notify
  29. {
  30. {
  31. boost::unique_lock<boost::mutex> lock(mx);
  32. data=t;
  33. }
  34. cond.notify_all();
  35. };
  36. T get() //get stored value
  37. {
  38. boost::unique_lock<boost::mutex> lock(mx);
  39. return data;
  40. }
  41. void waitWhileTrue() //waits until data is set to false
  42. {
  43. boost::unique_lock<boost::mutex> un(mx);
  44. while(data)
  45. cond.wait(un);
  46. }
  47. void waitWhile(const T &t) //waits while data is set to arg
  48. {
  49. boost::unique_lock<boost::mutex> un(mx);
  50. while(data == t)
  51. cond.wait(un);
  52. }
  53. void waitUntil(const T &t) //waits until data is set to arg
  54. {
  55. boost::unique_lock<boost::mutex> un(mx);
  56. while(data != t)
  57. cond.wait(un);
  58. }
  59. };