1

我最近在 Conway 的 Game of Life 中创建了一个模式搜索程序,但运行速度太慢,不实用。
所以我决定并行化它,但我失败了;它导致了分段错误,这很可能是由于数据竞争。
代码的简要说明:

/* ... */
#include <list>
#include <mutex>
#include <future>
#include <iostream>
#include <forward_list>
int main() {
    /* ... */
    while (true) {
        /* ... */
        std::forward_list</*pattern type*/> results;
        std::list<std::future<void>> results_future;
        std::mutex results_mutex;
        for (/*All the possible unique pattern in given grid*/)
            results_future.push_back(std::async([&]{
                /*A very hard calculation*/
                if (/*The pattern is what I'm looking for*/) {
                    std::lock_guard<std::mutex> results_guard(results_mutex);
                    results.push_front(std::move(/*The pattern*/));
                }
            }));
        while (!results_future.empty()) {
            results_future.front().wait();
            results_future.pop_front();
        }
        if (!results.empty()) {
            for (auto &res : results)
                std::cout << "Pattern found:" << std::endl << res;
            return 0;
        }
    }
}

我很确定results这是在 lambda 表达式的函数范围之外声明并在那里修改的唯一对象,所以我用互斥锁锁定了它。
但数据竞赛仍然存在。那么是什么原因造成的呢?

4

1 回答 1

0

我发现问题与 lambda 捕获有关:

for (/*All the possible unique pattern in given grid*/)
    results_future.push_back(std::async([&]{
        /*pattern type*/ patt_orig = search_grid;
        /* ... */
    }));

search_grid,如上面 SingerOfTheFall 的评论中所述,是通过引用捕获的。并将其转换为 lambda 范围内的模式类型。问题是search_grid可以在将其转换为模式类型对其进行修改,反之亦然。数据竞赛!

转换必须在 lambda 捕获内:

for (/*All the possible unique pattern in given grid*/)
    results_future.push_back(std::async([&, patt_orig = (/*pattern type*/)search_grid]{
        /* ... */
    }));

现在一切都好了。

于 2017-02-04T12:55:27.610 回答