Runtime modes
Current thread, background thread and manual update.
Runtime modes
This chapter applies only to applications that need to continuously advance algorithm work such
as Retargeting. When only reading the raw pose/IMU, there is no need to
choose a runtime: after a successful start() call, data is published with valid device frames.
Automatic reconnection advances independently and does not require continuous
update() calls or a runner.
When scheduling is needed, choose one of three mutually exclusive run modes: the current thread,
a background thread, or manual update. next() and C++20 coroutines are ways to wait for a single
data item, not a fourth run mode.
Running continuously on the current thread
run_forever() runs on the current thread and suits command-line programs with a simple
lifecycle:
glove.start();
glove.run_forever();run_forever() suits programs that keep running on the current thread. The public command-line
examples end the process with Ctrl+C; when the application needs to stop it actively, use the
background thread mode below.
Running on a background thread
Use this when the main thread must continue running the UI, the application event loop, or safe-stop coordination:
After start() succeeds, call glove.run_background() to start the background run.
join() waits for the background run to finish and reports runtime errors. run_background()
does not implicitly connect or start devices.
While waiting for output, observe the background result by calling glove.join() from one application thread. When the background run ends early, request stop through the coroutine's std::stop_source to end pending data waits. Wait for the join observer thread before reading its error, and preserve the original background failure. On consumption completion or processing failure, request stop if still running, wait for the same join observer, and then close the domain and SDK. Complete cleanup on exception paths too; keep the C++ objects referenced by the join observer alive until its thread has finished. The updated async_runtime.cpp provides the complete implementation. See Migration for version applicability and upgrade actions.
Complete sources and build files: Download the async runtime examples. Follow the included README to configure the SDK, port, side and algorithm package.
Manual update
When the application owns its own scheduling loop, call update() repeatedly to perform one SDK
task scheduling pass:
const auto update = glove.update();
do_application_work();
const auto result = update.sleep();
if (result == qnbot::SleepResult::no_tasks) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}UpdateResult provides tick(), ran_task_count(), and has_next_task(). The last field only
reports whether a periodic deadline exists; do not use it to gate sleep(), because event-driven
work is not necessarily reflected by that field. Call sleep() once after every update(). It
waits for subsequent work based on the current time and returns SleepResult::ready, stopped,
or no_tasks. When the SDK is fully idle or the application must supply the next frame, yield
briefly after no_tasks to avoid busy polling. Each update result's wait token can be used only
once. Raw pose/IMU is published with device frames and does not depend on update().
Blocking wait and cancellation
next() returns a move-only NextOperation<T> that waits for the next data item after
registration succeeds:
auto pending = device.pose().next();
auto sample = pending.get();next() can be used after start(); for continuous data processing, subscribe() registered
before starting is still preferred. IMU is awaited in the same way as pose.
get() blocks until data arrives or the operation terminates, and completes internal
subscription cleanup before returning. Raw pose/IMU can wake the wait directly after start();
when waiting for retargeting output, the SDK scheduling must be advanced by background mode or
another thread. Use cancel() to cancel synchronously:
auto pending = device.pose().next();
pending.cancel();
try {
pending.get();
} catch (const std::system_error& error) {
if (error.code() != std::errc::operation_canceled) {
throw;
}
}NextOperation<T> can be consumed only once. Calling it again on a moved-from or already
consumed operation throws LifecycleError. For the overview of read methods, see
Data channels & read methods.
C++20 coroutines
After additionally linking qnbot::core_coro and including <qnbot/coro.hpp>, you can change an
existing single wait to co_await qnbot::coro::next(...):
qnbot::Sample<qnbot::GlovePose> sample =
co_await qnbot::coro::next(
device.pose().next(), executor, stop_token);executor is a std::shared_ptr<qnbot::coro::DeferredExecutor>. Its post() must satisfy the
following contract:
- It receives each task once and eventually executes it once.
- It returns quickly and does not wait for the task execution to finish.
- It does not execute the task inline on the callback thread that calls
post(). post()does not throw.
The NextOperation<T> passed in can still be consumed only once. A std::stop_token that has
already requested stop results in std::errc::operation_canceled.
The application must ensure that the relevant executor, stop token, and task objects stay valid
while the coroutine has not yet completed or been cancelled; do not concurrently destroy a
coroutine while an executor callback is still running. For complete, directly usable handling of
the real glove lifecycle, see async_runtime.cpp.
Closing the owning SDK or domain from other application logic ends the related data waits and coordinates with a pending background join. The application must still await its existing join observer and inspect the result. For ordinary shutdown, keep the stop, same join, then close order.
Temporary silence or connection recovery does not mean the background run has ended. Channel waits do not end merely because data is absent. Use the join result to observe background completion; a data-wait timeout is not a background completion result. Explicit stop()/start() follows the existing lifecycle: after stopping, wait for the previous background join to finish before restarting.
Calibration and frame input
These rules include automatic terminal calibration for external input.
Run mode and calibration policy are independent. Manual update() means the application advances the loop; the default Auto policy still lets the SDK organize capture and calibration. With auto_terminal, terminal guidance remains active across updates. The Manual calibration policy instead makes the application create capture and calibration explicitly; see the migration notes.
These startup rules apply to the Auto policy with auto_terminal when targets still need capture or calibration:
| Run mode and input | Return and advancement |
|---|---|
| Background, all pending inputs supplied by devices | Returns after the selected targets finish preparation |
| Background, at least one pending external input | run_background() starts background processing and returns; the application continues push_frame() while terminal guidance remains active |
| Background, mixed device and pending external input | Also returns first; keep supplying external frames |
| Manual loop | Call push_frame() first for external input, then update() and sleep(); guidance continues across updates |
Foreground run_forever() | Holds the calling thread until stopped; supply external frames from another thread or an independent input task |
Compatible valid calibration is reused. An already-ready external input does not change startup waiting for other pending device inputs.
Background startup returning does not mean calibration is complete. Keep feeding frames during preparation. The raw value returned by push_frame() is not the target algorithm result; read normal results from the corresponding target output after calibration takes effect. See External input.
Do not use has_next_task to decide whether to call sleep(): event-driven work can still advance while that field is false. Wait once and handle qnbot::SleepResult::no_tasks. It can mean the SDK needs the next application-supplied frame, rather than calibration being finished. The snippet assumes the device is started and the application provides a new frame on each iteration.
#include <chrono>
#include <thread>
device.push_frame(frame);
const auto update = glove.update();
const auto result = update.sleep();
if (result == qnbot::SleepResult::stopped) {
// End the application loop.
} else if (result == qnbot::SleepResult::no_tasks) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
// Continue with the next input frame.
}Terminal requirements, failure, and cleanup
- Pending background automatic preparation fails to start without an interactive console or when another interaction owns the terminal. Choose
externalfor a service, custom UI, or non-interactive environment and submit confirmations from the application. This does not supply input frames for you. - Observe capture
session_stateandfailurethroughcapture_progress(), and targetjob.stateandjob.failurethroughcalibration_progress(). Failure and cancellation states arefailedandcancelled. - Handle preparation failure or cancellation reported after startup through these progress channels. Do not only wait for output or expect every failure to be thrown by startup.
join()reports background runtime errors but does not replace capture/calibration progress handling. - Finish background use with
request_stop()→join()→close(), reaching close even when an earlier step throws. Clean up on exception paths as well. Close after a manual loop as well. - Retained terminal guidance for manual updates and application-fed background preparation is released when preparation becomes ready, capture fails or is cancelled, or calibration fails. Background exit and participating device stop/close also release it. Foreground interaction lasts until that execution exits. A calibration cancellation notification does not guarantee cleanup is complete: still stop and close. The disappearance of prompts is not an output-readiness signal.
See the example index for complete external-input examples. They default to auto_terminal; add --capture-interaction external for application-owned prompts and confirmations.
SDK update notices at startup
Glove checks OTA at the first Glove start per SDK instance. The check looks for a newer stable version for the current binding and platform and waits at most one second. It prints the available version and upgrade link to standard error. It does not install updates. There is no separate Core check. If OTA is unreachable, normal startup continues. See Installation & requirements to upgrade.