Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

"pure virtual method called" error with std::thread and template function #16968

Open Quuxplusone opened 11 years ago

Quuxplusone commented 11 years ago
Bugzilla Link PR16969
Status NEW
Importance P normal
Reported by Alexander Rodin (rodin.alexander@gmail.com)
Reported on 2013-08-22 09:50:21 -0700
Last modified on 2013-08-22 09:50:21 -0700
Version 3.2
Hardware PC Linux
CC dgregor@apple.com, llvm-bugs@lists.llvm.org
Fixed by commit(s)
Attachments quick_sort.h (1326 bytes, text/x-chdr)
Blocks
Blocked by
See also
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
Quuxplusone commented 11 years ago

Attached quick_sort.h (1326 bytes, text/x-chdr): quick_sort function template