使用来自
the comments
,以下是我所做的:
#include <stdio.h>
#include <functional>
void DispatchToGuiThread(std::function<void(void)> task) {
puts("in gui thread");
task();
}
template <class Func>
struct GUIDispatchedTask
{
private:
Func _func;
public:
GUIDispatchedTask(Func func)
: _func(std::move(func))
{}
template <class ...T>
void operator()(T... args) {
DispatchToGuiThread([this, ...args = args] {
_func(args...);
});
}
};
void foo(int a, int b, int c) {
printf("a = %d\nb = %d\nc = %d\n", a, b, c);
}
int main() {
auto guiFoo = GUIDispatchedTask{foo};
guiFoo(1, 2, 3);
}
Demo