C++ Thread: terminate called without an active exception Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of C++ Thread: terminate called without an active exception without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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.

Answer

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.

We are here to answer your question about C++ Thread: terminate called without an active exception - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji