【问题标题】:How do I use the pthreads in a ROS C++ Node如何在 ROS C++ 节点中使用 pthread
【发布时间】:2021-04-28 12:58:45
【问题描述】:

我正在尝试在 ros 节点中使用 pthread 库,我将其包含为 #include <pthread>。当我运行 catkin_make 时,我收到以下错误。我创建了一个简单的线程std::thread m1(move, 1);

thread: No such file or directory
 #include <pthread>
          ^~~~~~~~~

移动函数的签名是void move(short axis_no, short direction = 0),我将线程实例化为

std::thread m1(move, 1);    
m1.join();

我尝试在我的 CmakeLists.txt 中添加 pthread 库,如下所示 add_compile_options(-std=c++11 -pthread)target_link_libraries(pthread)。有没有办法可以在 ros 节点中使用 pthread 库?

谢谢。

【问题讨论】:

  • pthread.h 也许?但你可能想要&lt;thread&gt;
  • 没有pthread 标头;有pthread.h。其次,如果您使用的是兼容的 C++11 工具链,那么为什么要直接使用 pthreads 是一个谜,因为 &lt;thread&gt; 提供了丰富的功能集,可以让您摆脱 pthread.h 的疯狂。
  • 在现代 C++(C++11 或更高版本)程序中直接使用 pthread 吗?
  • #include &lt;thread&gt; 那么您的代码 std::thread m1(move, 1); 看起来不错。如果你使用 CMake,那么 set(CMAKE_CXX_STANDARD 11) ;如果你想告诉 gcc 使用 pthread 库,那么它是-lpthread
  • 我包含了thread,我收到了错误/usr/include/c++/7/thread:240:2: error: no matching function for call to ‘std::thread::_Invoker&lt;std::tuple&lt;void (*)(short int, short int)&gt; &gt;::_M_invoke(std::thread::_Invoker&lt;std::tuple&lt;void (*)(short int, short int)&gt; &gt;::_Indices)’ operator()()

标签: c++ pthreads ros


【解决方案1】:

pthread 是在某些平台上可用的 C 库,如果您想使用 pthread_* 函数,您需要 #include &lt;pthread.h&gt;

从 C++11 开始,在原生线程库之上有一个抽象层,如果您改为 #include &lt;thread&gt;,则可以使用它。

然后您可以使用std::thread - 但您仍需要与-pthread 链接。

您可以让CMake 查找并添加适当的库,而不是硬编码-pthread

find_package(Threads REQUIRED)
target_link_libraries(your_app PRIVATE Threads::Threads)

在您的程序中,move 函数需要两个参数(即使您有 direction 的默认值,函数签名也是 void(*)(short, short))。

#include <thread>
#include <iostream>

void move(short axis_no, short direction = 0) {
    std::cout << axis_no << ' ' << direction << '\n';
}

int main() {
    auto th = std::thread(move, 1, 2); // needs 2 arguments
    th.join();
}

如果需要,您可以创建一个只需要提供一个参数的重载,但在这种情况下,您还需要帮助编译器选择正确的重载。

#include <thread>
#include <iostream>

void move(short axis_no, short direction) { // no default value here
    std::cout << axis_no << ' ' << direction << '\n';
}

void move(short axis_no) { // proxy function
    move(axis_no, 0);      // where 0 is default
}

int main() {
    using one = void(*)(short);
    using two = void(*)(short, short);

    // use one of the above type aliases to select the proper overload:
    auto th = std::thread(static_cast<one>(move), 1);
    th.join();
}

【讨论】:

  • 我收到错误usr/include/c++/7/thread:240:2: error: no matching function for call to ‘std::thread::_Invoker&lt;std::tuple&lt;void (*)(short int, short int), int&gt; &gt;::_M_invoke(std::thread::_Invoker&lt;std::tuple&lt;void (*)(short int, short int), int&gt; &gt;::_Indices)’ operator()() ^~~~~~~~ 我不知道可能是什么问题
  • 您好 Ted,我刚刚更新了问题以包含该问题
  • @daRula 啊...该函数实际上需要 2 个参数
  • 哇,谢谢 Ted,我想因为其他变量是默认变量,所以没有必要。成功了
  • @daRula 不客气。好吧,它可能会被缓存。使用cmake 清理和再生。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-12
  • 1970-01-01
相关资源
最近更新 更多