C++线程:在没有活动异常的情况下终止调用
C++ Thread: terminate called without an active exception
我正在尝试创建一个不重复的整数数组。要获得长度超过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)
请帮忙。提前谢谢。
创建线程的代码会创建一个立即销毁的堆栈变量。你需要改变这个:
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);
}
然后稍后取消对删除行的注释。
在if
语句中创建线程。然后,您通过获取引用来将指针指向它。该指针不会使线程对象保持活动状态,而是在if
退出时调用对象的析构函数。
这意味着std::terminate
被调用来终止正在运行的线程,而您只剩下一个悬空指针。
相关文章:
- 如何在编译器方便的情况下在 C/C++ 中发布算术溢出异常
- 如何避免字符串到整数转换情况下的无效参数异常
- 在存在异常的情况下使用for_each? std::exception_list
- C++:在没有活动异常(GCC)的情况下终止调用
- 在scoped_ptr发生异常的情况下未调用析构函数
- C++ 异常情况下的错误 C2228(".val"的左侧必须具有类/结构/联合)
- C++线程:在没有活动异常的情况下终止调用
- 'new'语句是否可以在不引发异常的情况下失败?
- 如何在不调试符号的情况下确定从外部库引发的异常
- C++:检查是否在没有外部库的情况下抛出了某个异常类型
- 在什么情况下,if语句可以抛出异常
- catch语句如何在没有反射的情况下识别异常类型
- 应用程序在未附加调试器的情况下引发异常
- 如何在不将字符串对象嵌入自定义异常类的情况下重新格式化错误消息
- Android OpenCV imread 标志致命异常,imread 在没有标志的情况下工作正常,像素输出与 MATLAB 不匹配
- 在没有活动异常的情况下调用c++终止
- 在没有活动异常线程的情况下终止调用
- 为什么 lock() 在死锁的情况下不抛出异常
- 在低内存情况下使用带有异常处理的STL容器
- 不同的异常在相同的情况下MFC/Sql