Skip to content

Commit

Permalink
Use std::thread in tests (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
apolukhin authored Dec 30, 2024
1 parent 816955c commit e7ad58b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 23 deletions.
5 changes: 3 additions & 2 deletions example/tutorial5/load_all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
#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
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_;
Expand Down
2 changes: 1 addition & 1 deletion test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down
77 changes: 57 additions & 20 deletions test/shared_library_concurrent_load_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>

#include <boost/filesystem/path.hpp>

#include <boost/core/lightweight_test.hpp>

namespace {

typedef std::vector<boost::dll::fs::path> paths_t;
const std::size_t thread_count = 4;
boost::barrier b(thread_count);

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[]) {
Expand All @@ -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]);
Expand All @@ -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);
Expand All @@ -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));
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));
}
threads.join_all();

for (auto& t: threads) {
t.join();
}
return boost::report_errors();
}

#endif // #ifdef BOOST_TRAVISCI_BUILD

0 comments on commit e7ad58b

Please sign in to comment.