【问题标题】:C++ Thread: terminate called without an active exceptionC++ 线程:在没有活动异常的情况下终止调用
【发布时间】:2016-05-04 10:04:58
【问题描述】:

我正在尝试创建一个整数数组而不重复。要获得长度超过 1000 的数组,需要花费大量时间。所以,我认为使用线程将是一个不错的决定。但是我写错了。到目前为止,以下是我的代码:

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;
}

然后调用它:

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;
}

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

但是,每当我通过./a.out 10 运行程序时,它都会通过给出输出来终止:

terminate called without an active exception
Aborted (core dumped)

请帮忙。提前致谢。

【问题讨论】:

标签: c++ multithreading c++11


【解决方案1】:

创建线程的代码会创建一个立即销毁的堆栈变量。你需要改变这个:

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

到这里:

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

然后在稍后取消注释您的删除行。

【讨论】:

    【解决方案2】:

    您在if 语句中创建线程。然后通过获取引用来推送指向它的指针。该指针不会使线程对象保持活动状态,而是在退出 if 时调用对象的析构函数。

    这意味着std::terminate is called 会终止正在运行的线程,而你会留下一个悬空指针。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-09
      • 2021-07-02
      • 2016-10-07
      • 1970-01-01
      • 2020-03-06
      • 2021-10-10
      • 2014-10-14
      • 1970-01-01
      相关资源
      最近更新 更多