[llvm-bugs] [Bug 49621] New: std::shuffle() hangs with clang but works well with gcc/msvs

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Mar 17 13:45:25 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=49621

            Bug ID: 49621
           Summary: std::shuffle() hangs with clang but works well with
                    gcc/msvs
           Product: new-bugs
           Version: unspecified
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: yury.norov at gmail.com
                CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org,
                    mikkqu at gmail.com

Confirmed on Linux and Mac. Discovered with Michail Kalashnikov.

clang -v
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Applications/

Reproducer: https://rextester.com/ZMML24952

#include <iostream>
#include <vector>
#include <algorithm>
#include <random>

using namespace std;

int shuffler_fn(int i) {
    //cout << "Shuffler: " << i << endl;
    return std::rand() % i;
}

void random_shuffle() {
    vector<int> rows{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

    std::random_shuffle(rows.begin(), rows.end(), shuffler_fn);

    for (const auto& item : rows) {
        cout << item << ' ';
    }
}

class Shuffler {
public:
  typedef int result_type;
  static constexpr int min() { return 0; }
  static constexpr int max() { return RAND_MAX; }

  int operator()() const { return std::rand(); }
};

void just_shuffle() {
    vector<int> rows{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

    Shuffler shuffler;

    std::shuffle(rows.begin(), rows.end(), shuffler);

    for (const auto& item : rows) {
        cout << item << ' ';
    }
}

int main()
{
    random_shuffle();
    cout << endl;
    just_shuffle();
    cout << endl;
    return 0;
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210317/325bef95/attachment-0001.html>


More information about the llvm-bugs mailing list