threading_models_test.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // thread_safe_signals library
  2. // basic test for alternate threading models
  3. // Copyright Frank Mori Hess 2008
  4. // Use, modification and
  5. // distribution is subject to the Boost Software License, Version
  6. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. // For more information, see http://www.boost.org
  9. // note boost/test/minimal.hpp can cause windows.h to get included, which
  10. // can screw up our checks of _WIN32_WINNT if it is included
  11. // after boost/signals2/mutex.hpp. Frank Hess 2009-03-07.
  12. #include <boost/test/minimal.hpp>
  13. #include <boost/signals2.hpp>
  14. #include <boost/thread/mutex.hpp>
  15. // combiner that returns the number of slots invoked
  16. struct slot_counter {
  17. typedef unsigned result_type;
  18. template<typename InputIterator>
  19. unsigned operator()(InputIterator first, InputIterator last) const
  20. {
  21. unsigned count = 0;
  22. for (; first != last; ++first)
  23. {
  24. try
  25. {
  26. *first;
  27. ++count;
  28. }
  29. catch(const boost::bad_weak_ptr &)
  30. {}
  31. }
  32. return count;
  33. }
  34. };
  35. void myslot()
  36. {
  37. }
  38. template<typename signal_type>
  39. void simple_test()
  40. {
  41. signal_type sig;
  42. sig.connect(typename signal_type::slot_type(&myslot));
  43. BOOST_CHECK(sig() == 1);
  44. sig.disconnect(&myslot);
  45. BOOST_CHECK(sig() == 0);
  46. }
  47. class recursion_checking_dummy_mutex
  48. {
  49. int recursion_count;
  50. public:
  51. recursion_checking_dummy_mutex(): recursion_count(0)
  52. {}
  53. void lock()
  54. {
  55. BOOST_REQUIRE(recursion_count == 0);
  56. ++recursion_count;
  57. }
  58. bool try_lock()
  59. {
  60. lock();
  61. return true;
  62. }
  63. void unlock()
  64. {
  65. --recursion_count;
  66. BOOST_REQUIRE(recursion_count == 0);
  67. }
  68. };
  69. int test_main(int, char*[])
  70. {
  71. typedef boost::signals2::signal<void (), slot_counter, int, std::less<int>, boost::function<void ()>,
  72. boost::function<void (const boost::signals2::connection &)>, recursion_checking_dummy_mutex> sig0_rc_type;
  73. simple_test<sig0_rc_type>();
  74. typedef boost::signals2::signal<void (), slot_counter, int, std::less<int>, boost::function<void ()>,
  75. boost::function<void (const boost::signals2::connection &)>, boost::mutex> sig0_mt_type;
  76. simple_test<sig0_mt_type>();
  77. typedef boost::signals2::signal<void (), slot_counter, int, std::less<int>, boost::function<void ()>,
  78. boost::function<void (const boost::signals2::connection &)>, boost::signals2::dummy_mutex> sig0_st_type;
  79. simple_test<sig0_st_type>();
  80. return 0;
  81. }