test_barrier_size_fct.cpp 1.2 KB

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