test_barrier.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (C) 2001-2003
  2. // William E. Kempf
  3. // (C) Copyright 2013 Vicente J. Botet Escriba
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #define BOOST_THREAD_PROVIDES_INTERRUPTIONS
  8. #include <boost/thread/detail/config.hpp>
  9. #include <boost/thread/thread.hpp>
  10. #include <boost/thread/barrier.hpp>
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <vector>
  13. namespace {
  14. // Shared variables for generation barrier test
  15. const int N_THREADS=3;
  16. boost::barrier gen_barrier(N_THREADS);
  17. boost::mutex mutex;
  18. long global_parameter;
  19. void barrier_thread()
  20. {
  21. for (int i = 0; i < 5; ++i)
  22. {
  23. if (gen_barrier.wait())
  24. {
  25. boost::unique_lock<boost::mutex> lock(mutex);
  26. global_parameter++;
  27. }
  28. }
  29. }
  30. } // namespace
  31. void test_barrier()
  32. {
  33. boost::thread_group g;
  34. global_parameter = 0;
  35. try
  36. {
  37. for (int i = 0; i < N_THREADS; ++i)
  38. g.create_thread(&barrier_thread);
  39. g.join_all();
  40. }
  41. catch(...)
  42. {
  43. BOOST_TEST(false);
  44. g.interrupt_all();
  45. g.join_all();
  46. //throw;
  47. }
  48. BOOST_TEST(global_parameter==5);
  49. }
  50. int main()
  51. {
  52. test_barrier();
  53. return boost::report_errors();
  54. }