在使用 测试某些功能时std::thread
,一位朋友遇到了 GCC 的问题,我们认为值得询问这是否是 GCC 错误或者这段代码可能有问题(代码打印(例如)“7 8 9 10 1 2 3” ,但我们希望打印 [1,10] 中的每个整数):
#include <algorithm>
#include <iostream>
#include <iterator>
#include <thread>
int main() {
int arr[10];
std::iota(std::begin(arr), std::end(arr), 1);
using itr_t = decltype(std::begin(arr));
// the function that will display each element
auto f = [] (itr_t first, itr_t last) {
while (first != last) std::cout<<*(first++)<<' ';};
// we have 3 threads so we need to figure out the ranges for each thread to show
int increment = std::distance(std::begin(arr), std::end(arr)) / 3;
auto first = std::begin(arr);
auto to = first + increment;
auto last = std::end(arr);
std::thread threads[3] = {
std::thread{f, first, to},
std::thread{f, (first = to), (to += increment)},
std::thread{f, (first = to), last} // go to last here to account for odd array sizes
};
for (auto&& t : threads) t.join();
}
以下替代代码有效:
int main()
{
std::array<int, 10> a;
std::iota(a.begin(), a.end(), 1);
using iter_t = std::array<int, 10>::iterator;
auto dist = std::distance( a.begin(), a.end() )/3;
auto first = a.begin(), to = first + dist, last = a.end();
std::function<void(iter_t, iter_t)> f =
[]( iter_t first, iter_t last ) {
while ( first != last ) { std::cout << *(first++) << ' '; }
};
std::thread threads[] {
std::thread { f, first, to },
std::thread { f, to, to + dist },
std::thread { f, to + dist, last }
};
std::for_each(
std::begin(threads),std::end(threads),
std::mem_fn(&std::thread::join));
return 0;
}
我们认为它可能与函数的无序评估有关,或者它只是在复制非限定参数std::thread
时应该工作的方式。std::ref
然后我们用 Clang 测试了第一个代码,它可以工作(因此开始怀疑是 GCC 错误)。
使用的编译器:GCC 4.7、Clang 3.2.1
编辑: GCC 代码在代码的第一个版本中给出了错误的输出,但在第二个版本中它给出了正确的输出。