shared_library_concurrent_load_test.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2015-2019 Antony Polukhin
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. // For more information, see http://www.boost.org
  7. #ifdef BOOST_TRAVISCI_BUILD
  8. int main() {
  9. return 0;
  10. }
  11. #else // #ifdef BOOST_TRAVISCI_BUILD
  12. #include "../example/b2_workarounds.hpp"
  13. #include <boost/dll.hpp>
  14. #include <boost/filesystem/path.hpp>
  15. #include <boost/thread/thread.hpp>
  16. #include <boost/thread/barrier.hpp>
  17. #include <boost/core/lightweight_test.hpp>
  18. #include <boost/bind.hpp>
  19. #include <cctype>
  20. #include <vector>
  21. typedef std::vector<boost::dll::fs::path> paths_t;
  22. const std::size_t thread_count = 4;
  23. boost::barrier b(thread_count);
  24. // Disgusting workarounds for b2 on Windows platform
  25. inline paths_t generate_paths(int argc, char* argv[]) {
  26. paths_t ret;
  27. ret.reserve(argc - 1);
  28. for (int i = 1; i < argc; ++i) {
  29. boost::dll::fs::path p = argv[i];
  30. if (b2_workarounds::is_shared_library(p)) {
  31. ret.push_back(p);
  32. }
  33. }
  34. return ret;
  35. }
  36. inline void load_unload(const paths_t& paths, std::size_t count) {
  37. for (std::size_t j = 0; j < count; j += 2) {
  38. for (std::size_t i = 0; i < paths.size(); ++i) {
  39. boost::dll::shared_library lib(paths[i]);
  40. BOOST_TEST(lib);
  41. }
  42. for (std::size_t i = 0; i < paths.size(); ++i) {
  43. boost::dll::shared_library lib(paths[i]);
  44. BOOST_TEST(lib.location() != "");
  45. }
  46. // Waiting for all threads to unload shared libraries
  47. b.wait();
  48. }
  49. }
  50. int main(int argc, char* argv[]) {
  51. BOOST_TEST(argc >= 3);
  52. paths_t paths = generate_paths(argc, argv);
  53. BOOST_TEST(!paths.empty());
  54. std::cout << "Libraries:\n\t";
  55. std::copy(paths.begin(), paths.end(), std::ostream_iterator<boost::dll::fs::path>(std::cout, ", "));
  56. std::cout << std::endl;
  57. boost::thread_group threads;
  58. for (std::size_t i = 0; i < thread_count; ++i) {
  59. threads.create_thread(boost::bind(load_unload, paths, 1000));
  60. }
  61. threads.join_all();
  62. return boost::report_errors();
  63. }
  64. #endif // #ifdef BOOST_TRAVISCI_BUILD