2017-09-13 62 views
0

嘿,我是新來的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; 

} 
+2

通過您的代碼行與調試器步進當行什麼你有沒有注意到? – user0042

+0

'std :: ref(A)'看起來不正確。你堅持使用引用,所以你不應該需要它。 – NathanOliver

+0

@ user0042我在windows上使用geany,我不認爲Geany允許調試模式。 – Moi

回答

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); 
    } 
} 
相關問題