gpt4 book ai didi

c++ - 包装 std::thread 调用函数

转载 作者:行者123 更新时间:2023-11-27 23:48:55 25 4
gpt4 key购买 nike

我想控制线程,所以每个新发出的线程都会先通过我的代码。
这样,在下面的示例中使用 runThread() 发出的每个新线程将首先调用下面的函数 runInnerThread()(我在其中进行了某种初始化)并且然后调用所需的函数。

我试过这样写:

#include <iostream>
#include <thread>

template<typename _Callable, typename... _Args>
void runThreadInner(_Callable&& __f, _Args&&... __args) {
// My initializations...
__f(__args...);
// My finishing...
};

template<typename _Callable, typename... _Args>
bool runThread(_Callable&& __f, _Args&&... __args) {
std::thread t(std::bind(&runThreadInner,
std::forward<_Callable>(__f),
std::forward<_Args>(__args)...));
}

int main() {
runThread([]() {
std::cout << std::this_thread::get_id() << "Threading...\n";
});
return 0;
}

我从编译器那里得到一个错误,它提示推导 runThreadInner() 模板

main.cpp: In instantiation of ‘bool runThread(_Callable&&, _Args&& ...) [with _Callable = main()::__lambda0; _Args = {}]’:
main.cpp:43:3: required from here
main.cpp:27:38: error: no matching function for call to ‘bind(<unresolved overloaded function type>, main()::__lambda0)’
std::forward<_Args>(__args)...));
^
main.cpp:27:38: note: candidates are:
In file included from /usr/include/c++/4.8/memory:79:0,
from main.cpp:2:
/usr/include/c++/4.8/functional:1655:5: note: template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__or_<std::is_integral<typename std::decay<_Tp>::type>, std::is_enum<typename std::decay<_Tp>::type> >::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...)
bind(_Func&& __f, _BoundArgs&&... __args)
^
/usr/include/c++/4.8/functional:1655:5: note: template argument deduction/substitution failed:
main.cpp:27:38: note: couldn't deduce template parameter ‘_Func’
std::forward<_Args>(__args)...));
^
In file included from /usr/include/c++/4.8/memory:79:0,
from main.cpp:2:
/usr/include/c++/4.8/functional:1682:5: note: template<class _Result, class _Func, class ... _BoundArgs> typename std::_Bindres_helper<_Result, _Func, _BoundArgs>::type std::bind(_Func&&, _BoundArgs&& ...)
bind(_Func&& __f, _BoundArgs&&... __args)
^
/usr/include/c++/4.8/functional:1682:5: note: template argument deduction/substitution failed:
main.cpp:27:38: note: couldn't deduce template parameter ‘_Result’
std::forward<_Args>(__args)...));

我尝试显式定义模板,但没有成功:

template<typename _Callable, typename... _Args>
bool runThread(_Callable&& __f, _Args&&... __args) {
std::thread t(std::bind(&runThreadInner<_Callable, _Args>,
std::forward<_Callable>(__f),
std::forward<_Args>(__args)...));
}

这可能吗?谢谢。

最佳答案

简单易行。

#include <thread>
#include <iostream>

template<typename Callable, typename... Args>
void runThreadInner(Callable&& f, Args&&... args) {
// My initializations...
f(args...);
// My finishing...
};

template<typename Callable, typename... Args>
std::thread runThread(Callable&& f, Args&&... args) {
std::thread t(&runThreadInner<Callable, Args...>,
std::forward<Callable> (f), std::forward<Args>(args)...);
return t;
}
int main() {
auto t = runThread([]() {
std::cout << "Threading..." << std::endl;
});
t.join();
return 0;
}

关于c++ - 包装 std::thread 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48268322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com