加入话题失败

问题描述 投票:0回答:1

我正在编写一个程序,它创建一些随机数组,然后使用两个不同的排序函数对它们进行排序。首先,我创建一个由不同大小的

vector
对象组成的
Fleet
。然后,我为每个队列创建一个线程数组,并在每个线程中放置一个 lambda 函数来复制队列并对其进行排序。
它工作正常,但当涉及到连接线程时,我得到:

“进程已完成,退出代码 -1073741819 (0xC0000005)”。

它成功加入第一个线程,但无法加入第二个线程。可能是什么问题?

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

using namespace std;

class Fleet {
private:
    int fleet_size;
    vector<int> fleet;
    int rnd_lower = 100;
    int rnd_upper = 1000;
public:
    Fleet(int size) {
        fleet_size = size;
        fleet.resize(fleet_size);

        random_device rd;
        mt19937 gen(rd());
        uniform_int_distribution<int> dist(rnd_lower, rnd_upper);

        for (int i = 0; i < size; ++i) {
            fleet[i] = dist(rd);
        }
    }
    void Print() {
        if (fleet_size < 30) {
            for (int i = 0; i < fleet_size; ++i) {
                cout << fleet[i] << " ";
            }
        }
        else {
            for (int i = 0; i < fleet_size; ++i) {
                if (i == 4) {
                    cout << ". . . ";
                    i = fleet_size - 4;
                }
                cout << fleet[i] << " ";
            }
        }
        cout << endl;
    }
    void LSDSort();
    void BucketSort();
};

int main() {
    vector<Fleet> fleets;
    for (int i = 100, j = 2; i < 1000000; i *= j) {
        fleets.emplace_back(i);
    }

    void (Fleet::*sortingFunctions[])() {
            &Fleet::BucketSort,
            &Fleet::LSDSort
    };

    int sortings = sizeof(sortingFunctions) / sizeof(sortingFunctions[0]);

    for (int i = 0; i < fleets.size(); ++i) {
        thread threads[sortings];
        for (int j = 0; j < sortings; ++j) {
            threads[j] = thread([=]() {
                Fleet tempFleet = fleets[i];
                (tempFleet.*sortingFunctions[j])();
                tempFleet.Print();
            });
        }

        for (auto &thread: threads) {
            thread.join();
        }
    }

    return 0;
}

void Fleet::LSDSort() {
    int max_value = *max_element(fleet.begin(), fleet.end());
    int max_digits = log10(max_value) + 1;

    vector<int> sorted(fleet_size);

    for (int digit = 0; digit < max_digits; ++digit) {
        vector<int> count(10, 0);

        for (const auto &value : fleet) {
            int digit_value = (value / static_cast<int>(pow(10, digit))) % 10;
            count[digit_value]++;
        }

        for (int i = 1; i < 10; ++i) {
            count[i] += count[i - 1];
        }

        for (int i = fleet_size - 1; i >= 0; --i) {
            int digit_value = (fleet[i] / static_cast<int>(pow(10, digit))) % 10;
            sorted[count[digit_value] - 1] = fleet[i];
            count[digit_value]--;
        }

        fleet = sorted;
    }
}

void Fleet::BucketSort() {
    int num_buckets = 10;
    vector<vector<int>> buckets(num_buckets);

    int max_value = *max_element(fleet.begin(), fleet.end());
    int min_value = *min_element(fleet.begin(), fleet.end());
    int range = max_value - min_value + 1;

    for (const auto &value : fleet) {
        int bucket_index = (value - min_value) / (range / num_buckets);
        buckets[bucket_index].push_back(value);
    }

    fleet.clear();
    for (auto &bucket : buckets) {
        sort(bucket.begin(), bucket.end());
        fleet.insert(fleet.end(), bucket.begin(), bucket.end());
    }
}

尝试使用向量,更改 lambda 函数但没有成功。

c++ multithreading c++17
1个回答
0
投票

在线程对象上调用

join()
之前,添加检查该对象是否为
joinable
:

        for (auto &thread: threads) {
            if (thread.joinable())
                thread.join();
        }

因为,如果线程在

join()
调用之前完成,则会引发异常
invalid_argument

© www.soinside.com 2019 - 2024. All rights reserved.