c++ - 在没有事件异常的情况下终止调用(多线程合并排序)

标签 c++ multithreading c++11 mergesort

嘿,我是 C++ 的新手,我正在尝试创建多线程合并排序,但我一直收到此错误。 *当数组为 1000 个整数时,线程合并排序似乎有效,但是当我将数组初始化为更大的数字(例如 10000 个整数)时,它给了我这样的异常:“在没有事件异常的情况下终止调用” 非常感谢您的帮助! 下面是我的代码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <thread>
#include <pthread.h>
#include <ctime>// include this header 

using namespace std;


void shuffle(int *arr, size_t n)
{
    if (n > 1) 
    {
        size_t i;
        srand(time(NULL));
        for (i = 0; i < n - 1; i++) 
        {
          size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
          int t = arr[j];
          arr[j] = arr[i];
          arr[i] = t;
        }
    }
}


// A function to merge the two half into a sorted data.
void merge(int *a, int low, int high, int mid)
{
    // We have low to mid and mid+1 to high already sorted.
    int i, j, k, temp[high-low+1];
    i = low;
    k = 0;
    j = mid + 1;

    // Merge the two parts into temp[].
    while (i <= mid && j <= high)
    {
        if (a[i] < a[j])
        {
            temp[k] = a[i];
            k++;
            i++;
        }
        else
        {
            temp[k] = a[j];
            k++;
            j++;
        }
    }

    // Insert all the remaining values from i to mid into temp[].
    while (i <= mid)
    {
        temp[k] = a[i];
        k++;
        i++;
    }

    // Insert all the remaining values from j to high into temp[].
    while (j <= high)
    {
        temp[k] = a[j];
        k++;
        j++;
    }


    // Assign sorted data stored in temp[] to a[].
    for (i = low; i <= high; i++)
    {
        a[i] = temp[i-low];
    }
}

void mergeSort(int A[], int low, int high){

        if (low < high) {
        int mid = (low + high) / 2;
        thread sort_thread1(mergeSort,std::ref(A),low,mid);
        thread sort_thread2(mergeSort,std::ref (A), mid + 1, high);
        sort_thread1.join();
        sort_thread2.join();
        merge(A, low, high, mid);
    }
    return;
}
int main(){

    int size =10000;
    int A[size];
    for (int i=0; i<size; i++){
        A[i] = i;
    }
    shuffle(A, size);
    //for (int i=0; i<size; i++){//
      //  printf("%d ", A[i]);
    //}//
    int low = 0;
    int high = size-1;
    int start_s=clock();
    // the code you wish to time goes here
    mergeSort(A,low,high);

    int stop_s=clock();
    cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC) << endl;

    //for(int i = 0; i<size;i++){
        //cout << A[i] << endl;`
    //}
    return 0;

}

最佳答案

这段代码创建了太多线程。

当它不能创建更多线程时 std::thread 构造函数抛出异常。异常开始展开调用现有 std::thread 对象的析构函数的堆栈。 std::thread::~thread destructor is problematic because it calls std::terminate if the thread is joinable but has not been joined .

参见 Discussion about std::thread and RAII了解更多详情。

此代码的修复方法是:

  • 让当前线程忙于排序,而不是等待其他 2 个线程,从而将线程数减半。
  • 在不创建新线程的情况下对小数组进行排序。

例子:

void mergeSort(int A[], int low, int high) {
    if (low < high) {
        int mid = (low + high) / 2;
        if(high - mid > 500) {
            thread sort_thread1(mergeSort,std::ref(A), low, mid);
            mergeSort(A, mid + 1, high); // Keep this thread busy.
            sort_thread1.join();
        }
        else { // Sort small arrays using 1 thread only.
            mergeSort(A, low, mid);
            mergeSort(A, mid + 1, high);
        }
        merge(A, low, high, mid);
    }
}

关于c++ - 在没有事件异常的情况下终止调用(多线程合并排序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46201266/

相关文章:

C++11 lambda 函数不接受类方法

c++ - 如何使用递归类模板在 C++11 中写出元组的内容?

c++ - 如何获取与 SWIG 中的 C++ 对象关联的 *the* PyObject*?

c++ - Visual C++ 等同于 GCC 实验性并行 for_each?

c++ - 将内存清理器与 libstdc++ 一起使用

c++ - 具有继承的全局类比较

ios - XML 解析器正在阻塞主线程

具有模板化类型结构问题的 C++ 函数模板参数

multithreading - 从命令行在Minizinc中并行求解

大型数据库插入的时间戳之间的 Java 计数