如何在 ROS C++ 节点中使用 pthreads
How do I use the pthreads in a ROS C++ Node
我正在尝试在 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 Node 中使用 pthread 库吗?
谢谢。
pthread
是一些平台上可用的 C 库,如果您想使用 pthread_*
函数,您需要 #include <pthread.h>
.
自 C++11 起,在本机线程库之上有一个抽象层,如果您改为 #include <thread>
。
然后您可以使用 std::thread
- 但您仍然需要 link 和 -pthread
。
而不是硬编码 -pthread
,您可以让 CMake
查找并添加适当的库:
find_package(Threads REQUIRED)
target_link_libraries(your_app PRIVATE Threads::Threads)
在您的程序中,move
函数需要两个参数(函数签名是 void(*)(short, short)
,即使您有 direction
的默认值)。
#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();
}
如果需要,您可以创建一个仅需提供一个参数的重载,但在这种情况下,您还需要帮助编译器 select 正确的重载。
#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();
}
我正在尝试在 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 Node 中使用 pthread 库吗?
谢谢。
pthread
是一些平台上可用的 C 库,如果您想使用 pthread_*
函数,您需要 #include <pthread.h>
.
自 C++11 起,在本机线程库之上有一个抽象层,如果您改为 #include <thread>
。
然后您可以使用 std::thread
- 但您仍然需要 link 和 -pthread
。
而不是硬编码 -pthread
,您可以让 CMake
查找并添加适当的库:
find_package(Threads REQUIRED)
target_link_libraries(your_app PRIVATE Threads::Threads)
在您的程序中,move
函数需要两个参数(函数签名是 void(*)(short, short)
,即使您有 direction
的默认值)。
#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();
}
如果需要,您可以创建一个仅需提供一个参数的重载,但在这种情况下,您还需要帮助编译器 select 正确的重载。
#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();
}