[英]Wrap std::thread call function
我想控制threads
,因此每個新發出的thread
將首先通過我的代碼。
這樣,在下面的示例中,每個由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 <functional>
(其中已定義std::bind
)。
但是,在您的情況下,實際上並不需要std::bind
,因為您有lambda,在許多方面都更好 。
這是一個沒有std::bind
的工作示例:
#include <iostream>
#include <functional>
#include <thread>
#include <utility>
template <typename F>
struct MyTaskWrapper {
F f;
template <typename... T>
void operator()(T&&... args) {
std::cout << "Stuff before...\n";
f(std::forward<T>(args)...);
std::cout << "Stuff after...\n";
}
};
template <typename F, typename... Args>
void runThread(F&& f, Args&&... args) {
std::thread trd(MyTaskWrapper<F>{std::forward<F>(f)}, std::forward<Args>(args)...);
trd.join();
}
int main() {
runThread([] {
std::cout << "Threading...\n";
});
}
輸出:
Stuff before...
Threading...
Stuff after...
十分簡單。
#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;
}
您的代碼有問題。 我幾乎拒絕代碼審查中的每一行。
_Capital
不是合法的C ++。 __lower
也不是。 停止復制system和std標頭所做的事情,不允許這樣做。 他們之所以這樣做, 恰恰是因為您現在可以保證他們的代碼不會與您執行愚蠢的預處理程序操作(以任何合法方式)重疊。
template<typename Callable>
std::thread runThread(Callable&& f) {
std::thread t([=]{
// My initializations...
f();
// My finishing...
});
return t;
}
int main() {
auto t = runThread([]() {
std::cout << std::this_thread::get_id() << "Threading...\n";
});
t.join();
}
如果有人想綁定參數,請讓他們自己在調用runThread
時進行綁定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.