Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add arguments to configure number of threads #1693

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions rclcpp_components/src/component_container_mt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,52 @@
int main(int argc, char * argv[])
{
/// Component container with a multi-threaded executor.
rclcpp::init(argc, argv);
auto exec = std::make_shared<rclcpp::executors::MultiThreadedExecutor>();
auto vargv = rclcpp::init_and_remove_ros_arguments(argc, argv);
size_t number_of_threads{0};
rclcpp::Logger logger{rclcpp::get_logger("component_container_mt")};

if (vargv.size() > 1) {
for (auto itr = vargv.begin() + 1; itr != vargv.end(); ++itr) {
if (*itr == "--help" || *itr == "-h") {
RCLCPP_INFO_STREAM(
logger,
"Usage: component_container_mt --thread-num <number of thread>");
return 0;
} else if (*itr == "--thread-num" || *itr == "-t") {
if (itr == vargv.end() - 1) {
RCLCPP_ERROR_STREAM(
logger, "Empty thread number" << std::endl <<
"Usage: component_container_mt --thread-num <number of thread>");
return 0;
}
try {
const auto arg_number_of_threads{std::stoi(*(itr + 1))};
if (arg_number_of_threads > 0) {
number_of_threads = static_cast<size_t>(arg_number_of_threads);
RCLCPP_INFO_STREAM(logger, "Number of threads: " << number_of_threads);
} else if (arg_number_of_threads < 0) {
RCLCPP_ERROR_STREAM(logger, "Number of threads is minus: " << *(itr + 1));
return 0;
}
++itr;
} catch (const std::invalid_argument & ex) {
RCLCPP_ERROR_STREAM(logger, "Invalid number of threads: " << *(itr + 1));
return 0;
} catch (const std::out_of_range & ex) {
RCLCPP_ERROR_STREAM(logger, "Number of threads is out of range: " << *(itr + 1));
return 0;
}
} else {
RCLCPP_ERROR_STREAM(
logger, "Invalid argument" << std::endl <<
"Usage: component_container_mt --thread-num <number of thread>");
return 0;
}
}
}

auto exec = std::make_shared<rclcpp::executors::MultiThreadedExecutor>(
rclcpp::ExecutorOptions(), number_of_threads);
auto node = std::make_shared<rclcpp_components::ComponentManager>(exec);
exec->add_node(node);
exec->spin();
Expand Down