Open Quuxplusone opened 11 years ago
quick_sort.h
Created attachment 11084 quick_sort function template I've created a multithreaded quick sort function: #include <utility> #include <thread> template <typename Iterator> void quick_sort(Iterator begin, Iterator end, int tc = 4) { if (begin < end - 1) { Iterator middle = end - 1; Iterator left = begin; while (left != middle) { if (*left > *middle) { --middle; std::swap(*middle, middle[1]); if (middle != left) std::swap(middle[1], *left); } else ++left; } if (tc > 1) { std::thread left_thread(quick_sort<Iterator>, begin, middle, tc / 2); std::thread right_thread(quick_sort<Iterator>, middle + 1, end, tc / 2); left_thread.join(); right_thread.join(); } else { quick_sort(begin, middle, 1); quick_sort(middle + 1, end, 1); } } } When I run a test program with it, for example int main() { double data[] = {0.840188, 0.394383, 0.783099, 0.798440, 0.911647, 0.197551, 0.335223, 0.768230, 0.277775, 0.553970, }; quick_sort(data, sizeof(data) / sizeof(double)); } the program fails: $ ./sort terminate called after throwing an instance of 'std::system_error' what(): Operation not permitted Aborted (core dumped) This code works fine with g++ 4.7. clang information: $ clang++ --version Ubuntu clang version 3.2-1~exp9ubuntu1 (tags/RELEASE_32/final) (based on LLVM 3.2) Target: x86_64-pc-linux-gnu Thread model: posix
Attached quick_sort.h (1326 bytes, text/x-chdr): quick_sort function template
quick_sort.h
(1326 bytes, text/x-chdr)