2
0
mirror of https://github.com/boostorg/dll.git synced 2026-01-19 04:12:08 +00:00

Use std::thread in tests (#88)

This commit is contained in:
Antony Polukhin
2024-12-30 14:34:53 +03:00
committed by GitHub
parent 816955cc8d
commit e7ad58bfb9
3 changed files with 63 additions and 25 deletions

View File

@@ -13,8 +13,9 @@
#include <boost/dll/runtime_symbol_info.hpp> // for program_location()
#include <boost/dll/shared_library.hpp>
#include <boost/make_shared.hpp>
#include <boost/container/map.hpp>
#include <boost/filesystem.hpp>
#include <map>
#include <iostream>
//[plugcpp_plugins_collector_def
@@ -22,7 +23,7 @@ namespace dll = boost::dll;
class plugins_collector {
// Name => plugin
using plugins_t = boost::container::map<std::string, dll::shared_library>;
using plugins_t = std::map<std::string, dll::shared_library>;
boost::dll::fs::path plugins_directory_;
plugins_t plugins_;

View File

@@ -97,7 +97,7 @@ project
[ run ../example/tutorial9/tutorial9.cpp ]
# test for shared libraries
[ compile-fail section_name_too_big.cpp ]
[ run shared_library_concurrent_load_test.cpp /boost/thread//boost_thread : : library1 library2 my_plugin_aggregator refcounting_plugin : <link>shared ]
[ run shared_library_concurrent_load_test.cpp : : library1 library2 my_plugin_aggregator refcounting_plugin : <link>shared ]
[ run cpp_mangle_test.cpp : : cpp_plugin ]
[ run cpp_mangling.cpp ]
[ compile-fail cpp_mangling1_cf.cpp ]

View File

@@ -6,28 +6,53 @@
// For more information, see http://www.boost.org
#ifdef BOOST_TRAVISCI_BUILD
int main() {
return 0;
}
#else // #ifdef BOOST_TRAVISCI_BUILD
#include "../example/b2_workarounds.hpp"
#include <boost/dll.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/barrier.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/bind.hpp>
#include <cctype>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <thread>
#include <vector>
typedef std::vector<boost::dll::fs::path> paths_t;
const std::size_t thread_count = 4;
boost::barrier b(thread_count);
#include <boost/filesystem/path.hpp>
#include <boost/core/lightweight_test.hpp>
namespace {
typedef std::vector<boost::dll::fs::path> paths_t;
class simple_barrier {
std::mutex mutex_;
std::condition_variable cv_;
std::size_t generation_{0};
const std::size_t initial_count_;
std::size_t count_{initial_count_};
public:
explicit simple_barrier(std::size_t count) : initial_count_(count) {}
simple_barrier(simple_barrier&&) = delete;
simple_barrier(const simple_barrier&) = delete;
void wait() {
std::unique_lock<std::mutex> lock{mutex_};
const auto gen_snapshot = generation_;
if (--count_ == 0) {
++generation_;
count_ = initial_count_;
cv_.notify_all();
} else {
cv_.wait(lock, [this, gen_snapshot] {
return gen_snapshot != generation_;
});
}
}
};
// Disgusting workarounds for b2 on Windows platform
inline paths_t generate_paths(int argc, char* argv[]) {
@@ -44,7 +69,7 @@ inline paths_t generate_paths(int argc, char* argv[]) {
return ret;
}
inline void load_unload(const paths_t& paths, std::size_t count) {
inline void load_unload(const paths_t& paths, std::size_t count, simple_barrier& b) {
for (std::size_t j = 0; j < count; j += 2) {
for (std::size_t i = 0; i < paths.size(); ++i) {
boost::dll::shared_library lib(paths[i]);
@@ -60,6 +85,8 @@ inline void load_unload(const paths_t& paths, std::size_t count) {
}
}
} // namespace
int main(int argc, char* argv[]) {
BOOST_TEST(argc >= 3);
@@ -70,13 +97,23 @@ int main(int argc, char* argv[]) {
std::copy(paths.begin(), paths.end(), std::ostream_iterator<boost::dll::fs::path>(std::cout, ", "));
std::cout << std::endl;
boost::thread_group threads;
for (std::size_t i = 0; i < thread_count; ++i) {
threads.create_thread(boost::bind(load_unload, paths, 1000));
}
threads.join_all();
constexpr std::size_t threads_count =
#ifdef BOOST_TRAVISCI_BUILD
1
#else
4
#endif
;
simple_barrier barrier{threads_count};
std::thread threads[threads_count];
for (auto& t: threads) {
t = std::thread(load_unload, paths, 1000, std::ref(barrier));
}
for (auto& t: threads) {
t.join();
}
return boost::report_errors();
}
#endif // #ifdef BOOST_TRAVISCI_BUILD