我對這里發生的事情一無所知
這是我想要實作的簡單代碼示例:
主程式
#include <iostream>
#include <thread>
int main(int argc, char** argv) {
constexpr int SIZE = 10;
std::array<int, SIZE> arr{0};
auto add = []<typename T>(std::array<T, SIZE>& arr) {
for (int i = 0; i < SIZE; i )
arr[i] = i 1;
};
std::thread t1(std::ref(add), std::ref(arr));
t1.join();
return 0;
}
編譯命令:
g -std=c 20 -Wall main.cpp -pthread -o t1
錯誤:
In file included from /usr/include/c /11.1.0/stop_token:35,
from /usr/include/c /11.1.0/thread:40,
from main.cpp:2:
/usr/include/c /11.1.0/bits/std_thread.h: In instantiation of ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = std::reference_wrapper<main(int, char**)::<lambda(std::array<T, 10>&)> >; _Args = {std::reference_wrapper<std::array<int, 10> >}; <template-parameter-1-3> = void]’:
main.cpp:15:48: required from here
/usr/include/c /11.1.0/bits/std_thread.h:130:72: error: static assertion failed: std::thread arguments must be invocable after conversion to rvalues
130 | typename decay<_Args>::type...>::value,
| ^~~~~
/usr/include/c /11.1.0/bits/std_thread.h:130:72: note: ‘std::integral_constant<bool, false>::value’ evaluates to false
/usr/include/c /11.1.0/bits/std_thread.h: In instantiation of ‘struct std::thread::_Invoker<std::tuple<std::reference_wrapper<main(int, char**)::<lambda(std::array<T, 10>&)> >, std::reference_wrapper<std::array<int, 10> > > >’:
/usr/include/c /11.1.0/bits/std_thread.h:203:13: required from ‘struct std::thread::_State_impl<std::thread::_Invoker<std::tuple<std::reference_wrapper<main(int, char**)::<lambda(std::array<T, 10>&)> >, std::reference_wrapper<std::array<int, 10> > > > >’
/usr/include/c /11.1.0/bits/std_thread.h:143:29: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = std::reference_wrapper<main(int, char**)::<lambda(std::array<T, 10>&)> >; _Args = {std::reference_wrapper<std::array<int, 10> >}; <template-parameter-1-3> = void]’
main.cpp:15:48: required from here
/usr/include/c /11.1.0/bits/std_thread.h:252:11: error: no type named ‘type’ in ‘struct std::thread::_Invoker<std::tuple<std::reference_wrapper<main(int, char**)::<lambda(std::array<T, 10>&)> >, std::reference_wrapper<std::array<int, 10> > > >::__result<std::tuple<std::reference_wrapper<main(int, char**)::<lambda(std::array<T, 10>&)> >, std::reference_wrapper<std::array<int, 10> > > >’
252 | _M_invoke(_Index_tuple<_Ind...>)
| ^~~~~~~~~
/usr/include/c /11.1.0/bits/std_thread.h:256:9: error: no type named ‘type’ in ‘struct std::thread::_Invoker<std::tuple<std::reference_wrapper<main(int, char**)::<lambda(std::array<T, 10>&)> >, std::reference_wrapper<std::array<int, 10> > > >::__result<std::tuple<std::reference_wrapper<main(int, char**)::<lambda(std::array<T, 10>&)> >, std::reference_wrapper<std::array<int, 10> > > >’
256 | operator()()
| ^~~~~~~~
筆記:
如果我將 lambda 更改為:
auto add = []<typename T>(std::array<T, SIZE> arr)
并像這樣呼叫執行緒:
std::thread t1(std::ref(add), arr);
它編譯,但顯然陣列沒有改變,因為它是一個副本而不是一個參考
uj5u.com熱心網友回復:
std::reference_wrapper不是 a std::array,所以T不能推匯出來。
add(std::ref(arr)); 也不編譯。
你可能會用
std::thread t1([&](auto arg){ add(arg.get()); }, std::ref(arr));
演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/403452.html
標籤:
上一篇:更新變數的Python異步呼叫
