【发布时间】:2016-03-13 23:41:57
【问题描述】:
我在使用std::thread 将整数数组传递给函数时遇到了困难。似乎线程不喜欢它的数组部分。还有什么其他方法可以将数组传递给线程函数?
#include <thread>
#include <ostream>
using namespace std;
void process(int start_holder[], int size){
for (int t = 0; t < size; t++){
cout << start_holder[t] << "\n";
}
}
int main (int argc, char *argv[]){
int size = 5;
int holder_list[size] = { 16, 2, 77, 40, 12071};
std::thread run_thread(process,holder_list,size);
//std::ref(list) doesnt work either
//nor does converting the list to std::string then passing by std::ref
run_thread.join();
}
【问题讨论】:
-
如果你要为你的变量使用标准库名称,不要说
using namespace std;。其实还是不要说出来了。 -
这实际上是
std::thread的问题,还是int list[size] = { 16, 2, 77, 40, 12071};,尽管有VLA 扩展,但它不适用于Clang? -
好的,知道如何解决这个问题吗?
-
我认为这是 std::thread 的问题。我应该改用 pthread 并将数组放入结构中吗?
-
将
size的类型更改为const int。
标签: c++ arrays multithreading c++11