gpt4 book ai didi

c++ - 从多个线程调用 std::shuffle

转载 作者:行者123 更新时间:2023-11-28 01:33:27 25 4
gpt4 key购买 nike

<分区>

我正在尝试在多个线程中打乱 vector 的拷贝。我的代码:

int main(int argc, const char *argv[]) {
std::vector<int> rw{1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1};
std::mutex m;
auto multithread_shuffle = [rw, &m]() {
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::shuffle (rw.begin(), rw.end(), std::default_random_engine(seed));
std::lock_guard lock(m);
for (size_t i = 0; i < rw.size(); i++) {
std::cout << "rw[" << i << "] = " << rw[i] << std::endl;
}
};
std::thread t1(multithread_shuffle);
std::thread t2(multithread_shuffle);
t1.join();
t2.join();
return 0;
}

但不幸的是,我收到了很多警告和这两个错误:

error: no matching function for call to 'swap(const int&, const int&)'
swap(*__a, *__b);
~~~~^~~~~~~~~~~~
error: no type named 'type' in 'struct std::enable_if<false, void>'

如有任何建议,我将不胜感激。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com