[英]How to pass multiple arguments to spawned threads (Mbed)?
I use Mbed RTOS and I need to pass multiple arguments to the threaded function.我使用 Mbed RTOS,我需要将多个 arguments 传递给线程 function。
There is an example in API docs: API 文档中有一个示例:
#include "mbed.h"
Thread thread;
DigitalOut led1(LED1);
volatile bool running = true;
// Blink function toggles the led in a long running loop
void blink(DigitalOut *led) {
while (running) {
*led = !*led;
wait(1);
}
}
// Spawns a thread to run blink for 5 seconds
int main() {
thread.start(callback(blink, &led1));
wait(5);
running = false;
thread.join();
}
But there is only one argument to pass.但只有一个论点可以通过。
Is there a convenient way to pass multiple arguments?有没有方便的方法传递多个arguments? I don't like the idea to put them into a struct and pass the struct.
我不喜欢将它们放入结构并传递结构的想法。 But I can't see any other way to do it.
但我看不到任何其他方法可以做到这一点。
You could use an std::tuple<>
instead of defining a new struct
.您可以使用
std::tuple<>
而不是定义新的struct
。 This is simpler in most cases, although as the number of argument members goes up, you might find yourself wanting a struct after all, to get names for them instead of having to std::get()
by type or index.在大多数情况下这更简单,尽管随着参数成员的数量增加,您可能会发现自己毕竟想要一个结构来获取它们的名称,而不是必须按类型或索引来
std::get()
。
There is unlikely to be any other way, as such callback systems typically only offer a single 'user data' pointer, and my brief googling about Mbed suggest it is the same.不可能有任何其他方式,因为这样的回调系统通常只提供一个“用户数据”指针,而我对 Mbed 的简短谷歌搜索表明它是相同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.