在没有活动异常的情况下终止调用(多线程合并排序)

时间:2017-09-13 15:21:10

标签: 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;

}

1 个答案:

答案 0 :(得分:1)

此代码创建了太多线程。

当它无法创建更多线程时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);
    }
}