C++ async:如果我不调用get(),那么std::shared_future的内存分配会发生什么?

huangapple go评论75阅读模式
英文:

c++ async: What happens to std::shared_future memory allocation if I don't call get()?

问题

在下面所示的异步调用之后,如果程序在不调用shared_future.get()的情况下退出会发生什么?我会有内存泄漏吗?

// 异步函数
std::shared_future<double*> sharedFutures = std::async(std::launch::async, myAsyncFunc, argument1, argument2);

if (realtimeCondition)
{
    // 退出后sharedFutures的内存分配会发生什么?
    // 内存泄漏吗?
    return 0;
}

// 获取结果
for (sharedFuture : sharedFutures)
    double* res = sharedFuture.get();
return 0;

如果程序在不调用shared_future.get()的情况下退出,将会释放sharedFutures对象,不会导致内存泄漏。内存泄漏通常在未释放动态分配的内存时发生,但在这种情况下,sharedFutures的生命周期将在退出时结束,它将自动释放分配的资源。

英文:

I am working on a simple idea and I came across this problem. What happens if after an async call as shown below the program exits without calling shared_future.get()?
Will I have a memory leak?

//async function
std::shared_future&lt;double*&gt; sharedFutures = std::async(std::launch::async, myAsyncFunc, argument1, argument2);

if (   realtimeCondition )
{
	//what happens to sharedFutures memory allocation after exiting
    //memory leak?
	return 0;
}

//getting results
for (sharedFuture : sharedFutures )
   double* res = sharedFuture.get();
return 0;

答案1

得分: 1

shared_future 在某种程度上类似于 std::shared_ptr。它们都管理共享资源。当最后一个 shared_x 被销毁时,资源会被释放。这在析构函数中进行检查(cppreference: https://en.cppreference.com/w/cpp/thread/shared_future/%7Eshared_future):

~shared_future(); (自C++11以来)

如果 *this 是最后一个引用共享状态的对象,则销毁共享状态。否则不执行任何操作。

如果一个类管理资源,它应该在析构函数中释放它。这对所有类都适用。这里的区别在于相同的资源在潜在地被多个对象之间共享。因此,每个对象在销毁资源之前必须检查是否是最后一个拥有该资源的对象。

因此,shared_future 析构函数在退出时调用,而不是在通过 get() 收集 futures 时调用吗?

如果对象在调用其方法时突然销毁自身,这将是不好且奇怪的行为。在这种情况下,无论何时访问它们管理的对象,shared_futureshared_ptr 都会变得几乎无用。在你的示例中,shared_future 在超出作用域时被销毁。

英文:

shared_future to some extend mimics std::shared_ptr. They both manage a shared resource. The resource is released when the last shared_x is destroyed. This is checked in the destructor (cppreference: https://en.cppreference.com/w/cpp/thread/shared_future/%7Eshared_future):

> ~shared_future(); (since C++11)
>
>If *this is the last object referring to the shared state, destroys the shared state. Otherwise does nothing.

If a class manages a resource it should release it in the destructor. Thats true for all classes. The difference here is that the same resource is shared between potentially many objects. So each object has to check if it is the last one to own the resource before it can destroy it.

> hence, shared_future destructors are called when exiting and not when having collected the futures via get()?

It would be bad and weird if an object suddenly destroys itself when you call one of its methods. Here specifically, both shared_future and shared_ptr would be practically useless when everytime you access the object they manage that object gets destroyed. The shared_future in your example gets destroyed when it goes out of scope.

答案2

得分: 0

如果我理解你的问题正确,在future上调用get()将会改变future的state->retrieved为true,但不会调用销毁操作。任何类型的future的销毁不依赖于其自身状态。

英文:

If I understand your question correctly calling get() on future will change state->retrieved of future to true but not calls destruction. Destruction of future of any type doesn't depend on its own state.

huangapple
  • 本文由 发表于 2023年2月8日 19:58:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定