概述
1.在使用C++11编写多线程程序的时候,出现了一个报错
2.已经在自己加的各种技术群里问了,没人回
3.上过stackoverflow 只有一个类似的问题,但是无法解决
链接:https://stackoverflow.com/que...
4.报错的代码段:
这个很诡异,没有说明具体是哪里错了,报错定位在整个工程的最后一行"}"字符处
但是报错是在我修改了一部分逻辑代码之后出现的,之前没有错误,运行正常
这段代码如下:
代码
c++11标准
int jump_while = false;
while (!jump_while) {
newTree.begin_flag = true;
//先写好一轮的代码,然后将其转化为循环,并思考终止条件
newTree.random_chooser(); //首先确定要随机选择的这一轮的传感器是哪些
cout << "Choosing phase successful" << endl; //完成选择
time_consumption += double(1.0 / newTree.get_frenquency()); //该轮时间计数开始
std::future *result = new std::future [newTree.source_node_sent_in_a_round]; //创建获取进程返回结果的future对象
vector thread_container;
for (int i = 0; i < newTree.source_node_sent_in_a_round; i++) {
int random_index = newTree.random_choosed[i];
packaged_task *pac = new packaged_task(&Tree::transmit);
thread *p = new thread(std::ref(*pac), newTree.vec[random_index]); //创建线程 , 防止被回收机制回收, 使用指针
thread_container.push_back(p); //防止内存泄露,收集以备释放
result[i] = (*pac).get_future(); //绑定future对象, 获取线程返回结果
(*p).join(); //所有线程阻塞主线程
}
newTree.begin_flag = true;
newTree.my_condition.notify_all(); //统一开始当前的这一局数据收集请求指令
for (int i = 0; i < newTree.source_node_sent_in_a_round; i++) {
Node* re = result[i].get(); //每一个线程开始尝试捕捉结果,并阻塞
if (re != nullptr) { //某一个线程获得返回值,在主线程开始判断
//计算得到结果,此时其他线程应该停止模拟???怎么实现?
jump_while = true;
die_first = re;
break;
}
}
for (int i = 0; i < newTree.source_node_sent_in_a_round; i++) {
delete thread_container[i]; //释放内存
}
}
补充一下引用的头文件和和结点类信息(stackoverflow上提供了)
#include
#include
#include
#include
#include
#include
#include
#include
#include //for memset
#include
#include
#include
#include
using namespace std;
class Node {
public:
int num;
double energy;
double x, y;
bool visited;
Node* father;
vector child;
int child_num;
Node(int& node_num, double energy, double x, double y) {
this->visited = false;
this->num = node_num;
this->energy = energy;
this->x = x;
this->y = y;
this->father = NULL;
this->child_num = 0;
node_num++;
}
~Node() {}
Node* remove_child(Node*);
void add_child(Node*);
};
Tree class信息:
class Tree {
public:
bool begin_flag;
mutex begin_lock;
vector my_lock;
condition_variable my_condition;
double THRESHOLD;
int level_num;
int SINK_ID; //from 0 to n - 1
Node* root;
double Eelec;
double Efs;
double Emp;
double k;
int frequency;
double initial_energy_for_sink_node;
double initial_energy_for_average_node;
vector c_vec;
double communication_range;
int source_node_sent_in_a_round;
int number_of_each_node_package;
int num_getter();
double get_distance(Node* p1, Node* p2);
bool able_conn(Node* p1, Node* p2, double communication_range);
double send_energy(Node* p1, Node* p2);
double receive_energy();
int get_average_children_num(int l);
bool verify_conn(Cordinate c1, Cordinate c2);
Cordinate random_helper();
Tree();
~Tree();
vector vec; //to storage node[i]'s info (vec[i] = node[i])
vector > v2d; //to storage node[i]'s able connected node (v2d[i][j] = node v2d[i][j] able to communicate with node[i])
vector > level; //to storage each level's node (level[i] represents for all nodes in the level[i])
vector node_in_tree_vec;
vector random_choosed;
void random_data_generator();
Node* buildTree();
int get_level_num();
int level_counter(Node* root);
Node* get_sink();
void levelizer(Node* root, int count);
Node* transmit(Node* source);
void random_chooser();
int get_frenquency();
void topDownBalancing();
void downTopBalancing();
void levelOrderTraverse();
void show_residual_energy();
int get_source_node_sent_in_a_round();
string name_getter();
};
报错信息(编译环境visual studio 2015)
报错信息(编译环境DEV-C++)
另外也使用过GCC 4.8.1 release编译(DEV-C++ 5.7.1)
报错信息较多一些:
希望得到大神的指点,多谢!
最后
以上就是贪玩冰淇淋为你收集整理的future 线程报错后_C++11多线程编程报错?(提示nullptr)的全部内容,希望文章能够帮你解决future 线程报错后_C++11多线程编程报错?(提示nullptr)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复