Arnab Das
Arnab Das

Reputation: 3738

C++ Thread: terminate called without an active exception

I'm trying to create one array of integers without repeat. To get arrays of length more than 1000, it takes a lot of time to make. So, I thought using thread would be a good decision. But I'm writing something wrong. So far following are my code:

utils.h

#ifndef UTILS_H
#define UTILS_H

typedef long long int64; typedef unsigned long long uint64;

class utils
{
    public:
        utils();
        virtual ~utils();
        static int getRandomNumberInRange(int min, int max);
        static int* getRandomArray(int size, bool isRepeatAllowed);

    protected:

    private:
};

#endif // UTILS_H

utils.cpp

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <vector>
#include <algorithm> // for std::find
#include <sys/time.h>
#include <cctype>
#include <string>
#include <thread>
#include <vector>

#include "utils.h"

utils::utils()
{

}

utils::~utils()
{

}
int utils::getRandomNumberInRange(int min, int max)
{
    if (min > max) {
        int aux = min;
        min = max;
        max = aux;
    }
    else if (min == max) {
        return min;
    }
    return (rand() % (max - min)) + min;
}

void getUniqueInteger(int* arr, int last, int* newVal)
{
    int val = *newVal;
    while(std::find(arr, arr+last, val) != arr+last)
    {
        val = utils::getRandomNumberInRange(10, 10000);
    }
    arr[last] = val;
}

int* utils::getRandomArray(int size, bool isRepeatAllowed)
{
    int* arr = new int[size], newVal = 0;
    std::vector<std::thread *> threadArr;

    for (int i = 0; i < size; i++)
    {
        newVal = utils::getRandomNumberInRange(10, 1000);
        if(!isRepeatAllowed)
        {
            std::thread newThread(getUniqueInteger, arr, i, &newVal);
            threadArr.push_back( &newThread);
        }
        else
        {
            arr[i] = newVal;
        }
    }

    int spawnedThreadCount = threadArr.size();

    if (spawnedThreadCount > 0)
    {
        for (int j = 0; j < spawnedThreadCount; j++)
        {
            threadArr[j]->join();
            //delete threadArr[j];
        }
    }

    return arr;
}

And calling this in:

main.cpp

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

#include "utils.h"

using namespace std;

int main(int argc, char *argv[])
{
  if (argc != 2 && utils::isInteger(argv[1]))
  {
    cout << "You have to provide an integer input to this program!!!" << endl;
    return 0;
  }

  int size = stoi( argv[1] );

  srand(time(NULL));

  int* arr = utils::getRandomArray(size, false);

  return 0;
}

Compiling by: g++ -Wall -g -std=c++11 -pthread -o a.out ./utils.cpp ./main.cpp

But, whenever I'm running the program by ./a.out 10, it's terminating by giving the output:

terminate called without an active exception
Aborted (core dumped)

Please help. Thanks in advance.

Upvotes: 0

Views: 8026

Answers (2)

Smeeheey
Smeeheey

Reputation: 10326

Your code that creates the thread creates a stack variable that is immediately destroyed. You need to change this:

    if(!isRepeatAllowed)
    {
        std::thread newThread(getUniqueInteger, arr, i, &newVal);
        threadArr.push_back( &newThread);
    }

to this:

    if(!isRepeatAllowed)
    {
        std::thread* newThread = new std::thread(getUniqueInteger, arr, i, &newVal);
        threadArr.push_back( newThread);
    }

Then uncomment your delete line later on.

Upvotes: 4

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31193

You create your thread inside the if statement. Then you push a pointer to it by getting a reference. This pointer will not keep the thread object alive, rather when the if is exited your object's destructor is called.

This means that std::terminate is called to terminate the running thread and you're left with a dangling pointer.

Upvotes: 2

Related Questions