thread_safety_checking.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright Antony Polukhin, 2016-2019.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include "test_impl.hpp"
  7. #include <boost/stacktrace/stacktrace_fwd.hpp>
  8. #include <sstream>
  9. #include <boost/stacktrace.hpp>
  10. #include <boost/thread.hpp>
  11. #include <boost/optional.hpp>
  12. #include <boost/core/lightweight_test.hpp>
  13. #include <boost/timer/timer.hpp>
  14. using boost::stacktrace::stacktrace;
  15. void main_test_loop() {
  16. std::size_t loops = 100;
  17. int Depth = 25;
  18. boost::optional<std::pair<stacktrace, stacktrace> > ethalon;
  19. std::stringstream ss_ethalon;
  20. while (--loops) {
  21. std::pair<stacktrace, stacktrace> res = function_from_library(Depth, function_from_main_translation_unit);
  22. if (ethalon) {
  23. BOOST_TEST(res == *ethalon);
  24. std::stringstream ss;
  25. ss << res.first;
  26. BOOST_TEST(ss.str() == ss_ethalon.str());
  27. } else {
  28. ethalon = res;
  29. ss_ethalon << ethalon->first;
  30. }
  31. }
  32. }
  33. #if defined(BOOST_STACKTRACE_TEST_COM_PREINIT_MT) || defined(BOOST_STACKTRACE_TEST_COM_PREINIT_ST)
  34. # include <windows.h>
  35. # include "dbgeng.h"
  36. #endif
  37. int main() {
  38. #if defined(BOOST_STACKTRACE_TEST_COM_PREINIT_MT)
  39. ::CoInitializeEx(0, COINIT_MULTITHREADED);
  40. #elif defined(BOOST_STACKTRACE_TEST_COM_PREINIT_ST)
  41. ::CoInitializeEx(0, COINIT_APARTMENTTHREADED);
  42. #endif
  43. boost::timer::auto_cpu_timer t;
  44. boost::thread t1(main_test_loop);
  45. boost::thread t2(main_test_loop);
  46. boost::thread t3(main_test_loop);
  47. main_test_loop();
  48. t1.join();
  49. t2.join();
  50. t3.join();
  51. #if defined(BOOST_STACKTRACE_TEST_COM_PREINIT_MT) || defined(BOOST_STACKTRACE_TEST_COM_PREINIT_ST)
  52. ::CoUninitialize();
  53. #endif
  54. return boost::report_errors();
  55. }