Skip to content

Commit

Permalink
Use call_once to initialize threading
Browse files Browse the repository at this point in the history
  • Loading branch information
RainerKuemmerle committed Jan 25, 2025
1 parent 3687609 commit 6b5b098
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
13 changes: 10 additions & 3 deletions g2o/core/optimizable_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <iostream>
#include <iterator>
#include <memory>
#include <mutex>
#include <sstream>
#include <unordered_set>
#include <utility>
Expand All @@ -57,6 +58,14 @@
namespace g2o {

namespace {
std::once_flag calledMultiThreadInit;

void initThreading() {
#if (defined G2O_OPENMP) && EIGEN_VERSION_AT_LEAST(3, 1, 0)
Eigen::initParallel();
#endif
}

std::shared_ptr<OptimizableGraph::Vertex> kNonExistantVertex(nullptr);

void saveUserData(AbstractGraph::AbstractGraphElement& graph_element,
Expand Down Expand Up @@ -791,9 +800,7 @@ void OptimizableGraph::addGraph(OptimizableGraph& other) {
}

bool OptimizableGraph::initMultiThreading() {
#if (defined G2O_OPENMP) && EIGEN_VERSION_AT_LEAST(3, 1, 0)
Eigen::initParallel();
#endif
std::call_once(calledMultiThreadInit, initThreading);
return true;
}

Expand Down
7 changes: 7 additions & 0 deletions unit_test/general/graph_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,15 @@ class JacobianWorkspaceTestAdapter : public g2o::JacobianWorkspace {
};
} // namespace

TEST(General, InitMultiThreading) {
bool init = false;
EXPECT_NO_THROW({ init = g2o::OptimizableGraph::initMultiThreading(); });
EXPECT_TRUE(init);
}

TEST(General, BinaryEdgeConstructor) {
g2o::EdgeSE2 e2;
ASSERT_THAT(e2.vertices(), SizeIs(2));
ASSERT_EQ(nullptr, e2.vertices()[0]);
ASSERT_EQ(nullptr, e2.vertices()[1]);
}
Expand Down

0 comments on commit 6b5b098

Please sign in to comment.