【问题标题】:Calling std::shuffle from multiple threads [duplicate]从多个线程调用 std::shuffle [重复]
【发布时间】:2018-11-04 02:24:37
【问题描述】:

我正在尝试在多个线程中随机播放向量的副本。我的代码:

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>'

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

【问题讨论】:

  • 你使用的是什么?
  • 这一切都将发生得如此之快,以至于两个线程将获得相同的种子并执行相同的洗牌的可能性非常大。

标签: c++ multithreading stl c++14 stdvector


【解决方案1】:

问题是向量在复制到 lambda 的捕获参数时变为const。考虑:

int main() {
    int x = 15;
    auto func = [x]{//Capture by value
        x = 20;//Does not compile!
    };
}

出于所有意图和目的,xconst int,而不是 int

您只需稍作改动即可在代码中遇到同样的问题:

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]() {
        rw[0] = 3; //Does not compile!
        rw = std::vector<int>{4,3,2,1}; //Does not compile!
        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;
}

如果您希望它是可变的,则需要显式分配该向量的本地副本。

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]() {
        auto rw_mutable = rw;
        unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
        std::shuffle (rw_mutable.begin(), rw_mutable.end(), std::default_random_engine(seed));
        std::lock_guard lock(m);
        for (size_t i = 0; i < rw.size(); i++) {
            std::cout << "rw_mutable[" << i << "] = " << rw_mutable[i] << std::endl;
        }
    };
    std::thread t1(multithread_shuffle);
    std::thread t2(multithread_shuffle);
    t1.join();
    t2.join();
    return 0;
}

你也可以只声明 lambda mutable:

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]() mutable {
        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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 2010-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多