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;
}
通過您的代碼行與調試器步進當行什麼你有沒有注意到? – user0042
'std :: ref(A)'看起來不正確。你堅持使用引用,所以你不應該需要它。 – NathanOliver
@ user0042我在windows上使用geany,我不認爲Geany允許調試模式。 – Moi