dimanche 5 avril 2015

C++: terminate called after throwing an instance of 'std::system_error' terminate called recursively

i'm trying to do a merge sort using threads from c++11, my program works fine with a vector of 20000 elements but when is >20000, that message of error appear **terminate called after throwing an instance of 'std::system_error' terminate called recursively terminate called recursively Abort('core' dumped) ** I just use 2 threads recursively.. Here is my code



#include <iostream>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <thread>
#include <mutex>
#include <iterator>

using namespace std;

mutex mtx;
void merge (int* array_n, int n_tam, int m_edio) {
// mtx.lock();
int i, j, k;
int *aux = new int [n_tam];
for (i = 0, j = m_edio, k = 0; k < n_tam; k++) {

if (j == n_tam)
aux[k] = array_n[i++];
else
if(i == m_edio)
aux[k] =array_n[j++];
else
if (array_n[j] < array_n[i])
aux[k] = array_n[j++];
else
aux[k] = array_n[i++];
}

for (i = 0; i < n_tam; i++) {
array_n[i] = aux[i];
}
delete [] aux;
// mtx.unlock();
}

void merge_sort (int* array_n, int tam) {
if (tam < 2)
return;

int medio = tam / 2;

thread first(merge_sort,array_n, medio);
thread second(merge_sort,array_n +medio , tam - medio);

first.join();
second.join();

merge(array_n, tam, medio);
}

int RandomNumbers(){ return rand()%10001;}

void imprimir(vector<int> v){
ostream_iterator<int> osit(cout," ");
copy(v.begin(), v.end(), osit);
cout << "\n";
}

int main () {
srand(time(NULL));
int n_tam = 60000;
vector<int> vec(n_tam);
generate(vec.begin(), vec.end(), RandomNumbers);
merge_sort(&vec[0],n_tam);
imprimir(vec);

return 0;
}


I tried with mutex, you can see the " // " but is the same.


Aucun commentaire:

Enregistrer un commentaire