Skip to content

Improve readibility of reported thread ids in the multithreaded executor example #415

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

Merged
merged 4 commits into from
May 11, 2025
Merged
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
12 changes: 10 additions & 2 deletions rclcpp/executors/multithreaded_executor/multithreaded_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <algorithm>
#include <chrono>
#include <functional>
#include <memory>
#include <string>
#include <thread>
#include <vector>

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
Expand All @@ -27,8 +29,14 @@ using namespace std::chrono_literals;
**/
std::string string_thread_id()
{
auto hashed = std::hash<std::thread::id>()(std::this_thread::get_id());
return std::to_string(hashed);
static std::vector<std::thread::id> known_thread_ids {};
const auto thread_it = std::find(known_thread_ids.cbegin(), known_thread_ids.cend(),
std::this_thread::get_id());
if (thread_it != known_thread_ids.cend()) {
return std::to_string(std::distance(known_thread_ids.cbegin(), thread_it));
}
known_thread_ids.push_back(std::this_thread::get_id());
return std::to_string(known_thread_ids.size() - 1);
}

/* For this example, we will be creating a publishing node like the one in minimal_publisher.
Expand Down