Skip to content

Commit

Permalink
Overloading the set_thread_affinity method for Windows compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
SENAI-GilmarCorreia committed Nov 12, 2024
1 parent c0d6b2b commit 06329ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions include/realtime_tools/realtime_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <string>
#include <thread>
#include <pthread.h>
#include <utility>

namespace realtime_tools
Expand Down
17 changes: 12 additions & 5 deletions src/realtime_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,16 @@ bool lock_memory(std::string & message)
#endif
}

std::pair<bool, std::string> set_thread_affinity(pthread_t thread, int core)
{
std::string message;
#ifdef _WIN32
message = "Thread affinity is not supported on Windows.";
std::pair<bool, std::string> set_thread_affinity(HANDLE thread, int core)
{
std::string message = "Thread affinity is not supported on Windows.";
return std::make_pair(false, message);
}
#else
std::pair<bool, std::string> set_thread_affinity(pthread_t thread, int core)
{
std::string message;
auto set_affinity_result_message = [](int result, std::string & msg) -> bool {
if (result == 0) {
msg = "Thread affinity set successfully!";
Expand Down Expand Up @@ -176,8 +179,8 @@ std::pair<bool, std::string> set_thread_affinity(pthread_t thread, int core)
" cores. Parsed core number should be between 0 and " +
std::to_string(number_of_cores - 1);
return std::make_pair(false, message);
#endif
}
#endif

std::pair<bool, std::string> set_thread_affinity(std::thread & thread, int core)
{
Expand All @@ -190,7 +193,11 @@ std::pair<bool, std::string> set_thread_affinity(std::thread & thread, int core)

std::pair<bool, std::string> set_current_thread_affinity(int core)
{
#ifdef _WIN32
return set_thread_affinity(GetCurrentThread(), core);
#else
return set_thread_affinity(pthread_self(), core);
#endif
}

int64_t get_number_of_available_processors()
Expand Down

0 comments on commit 06329ba

Please sign in to comment.