test_barrier_void_fct.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. void void_fct() {
  15. global_parameter++;
  16. }
  17. const int N_THREADS=3;
  18. boost::barrier gen_barrier(N_THREADS, &void_fct);
  19. void barrier_thread()
  20. {
  21. for (int i = 0; i < 5; ++i)
  22. {
  23. gen_barrier.count_down_and_wait();
  24. }
  25. }
  26. } // namespace
  27. void test_barrier()
  28. {
  29. boost::thread_group g;
  30. global_parameter = 0;
  31. try
  32. {
  33. for (int i = 0; i < N_THREADS; ++i)
  34. g.create_thread(&barrier_thread);
  35. g.join_all();
  36. }
  37. catch(...)
  38. {
  39. BOOST_TEST(false);
  40. g.interrupt_all();
  41. g.join_all();
  42. //throw;
  43. }
  44. BOOST_TEST(global_parameter==5);
  45. }
  46. int main()
  47. {
  48. test_barrier();
  49. return boost::report_errors();
  50. }