我正在动态创建线程:
auto thr =new std::future<void>(std::async(/*some callable*/));
我将所有这些指针存储在一个std::vector<future<void>*>
. 为了释放内存,我正在这样做:
for(int i(0);i<futureVector.size();i++)
{
if(futureVector.at(i)->valid())
{
futureVector.at(i)->get(); // for getting exception,if any
}
delete futureVector.at(i); // the problem is here
}
现在在我的代码中,分配给的内存可能futureVector.at(i)
已经被释放(可能在其他线程中,可能由其他函数)。我的问题是如何检测指针futureVector.at(i)
是否有效?我的意思是它指向有效std::future
与否?
注意:futureVector
变量是我的类的静态成员。
假设我不删除该future
对象的成本非常大(已经检索到未来)