# Future

```cpp
#include <qbruntime/future.h>

template <typename T>
class mobilint::Future
```

Represents a future for retrieving the result of asynchronous inference.

This class provides a mechanism similar to std::future, allowing access to the result of an asynchronous inference operation initiated via [Model::inferAsync](<https://docs.mobilint.com/runtime/en/v1.1/api/cpp/model/#inferasync>) or [Model::inferAsyncCHW](<https://docs.mobilint.com/runtime/en/v1.1/api/cpp/model/#inferasyncchw>).

The [Future](<https://docs.mobilint.com/runtime/en/v1.1/api/cpp/future/index.md>) object enables the caller to:

- Wait for the inference to complete (`waitFor`)
- Block until completion and retrieve the output (`get`)

Like std::future, this object can be moved but cannot be copied. It is associated with a unique asynchronous result. Once the output is retrieved via `get`, the [Future](<https://docs.mobilint.com/runtime/en/v1.1/api/cpp/future/index.md>) becomes invalid and should not be reused.

## Member functions

| Member | Description |
| --- | --- |
| [`Future`](<#future>) ×3 |  |
| [`operator=`](<#operator-assign>) ×2 |  |
| [`waitFor`](<#waitfor>) | Waits for asynchronous inference to complete or until the timeout elapses. |
| [`get`](<#get>) | Blocks until asynchronous inference completes and retrieves the output. |

### Future

```cpp
Future()
```

```cpp
Future(const Future &other)=delete
```

```cpp
Future(Future &&other) noexcept
```

### operator=

```cpp
Future & operator=(const Future &other)=delete
```

```cpp
Future & operator=(Future &&other) noexcept
```

### waitFor

```cpp
bool waitFor(int64_t timeout_ms)
```

Waits for asynchronous inference to complete or until the timeout elapses.

> [!NOTE]
>
> This method is safe to call multiple times. Calling it with a timeout of zero (i.e., `waitFor(0)`) performs a non-blocking check to see whether asynchronous inference has already completed.

**Parameters**

- `timeout` *\[in\]* — Maximum duration to wait, in milliseconds.

**Returns**

True if inference completed before the timeout, false otherwise.

### get

```cpp
std::vector<NDArray<T>> get(StatusCode &sc)
```

Blocks until asynchronous inference completes and retrieves the output.

> [!NOTE]
>
> This method should be called only once per [Future](<https://docs.mobilint.com/runtime/en/v1.1/api/cpp/future/index.md>). If called again, the return value will be invalid, and the status code will be set to [StatusCode::Future\_NotValid](<https://docs.mobilint.com/runtime/en/v1.1/api/cpp/#statuscode>).

**Parameters**

- `sc` *\[out\]* — A reference to a status code that will be updated to indicate whether the asynchronous inference completed successfully or if an error occurred.

**Returns**

A vector of [NDArray\<T\>](<https://docs.mobilint.com/runtime/en/v1.1/api/cpp/ndarray/index.md>) containing the inference output.
