При тестировании некоторых функций с помощью 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
-qualified аргументов. Затем мы проверили первый код с Clang, и он работает (и поэтому начал подозревать ошибку GCC).
Используется компилятор: GCC 4.7, Clang 3.2.1
EDIT: Код GCC дает неправильный вывод с первой версией кода, но со второй версией он дает правильный вывод.