# Asynchronous Interface Usage Guide

Header file:wmpf/operation.h

SDK A large number of asynchronous operations exist on the interface of the. If you see a function with a return value of typewx_operation_t, then the function is an asynchronous function and needs to call any of the following functions at the right time to wait for the result of asynchronous execution:

  • wx_operation_wait(): Synchronous blocking wait. Support setting timeout time.
  • wx_operation_await(): Waits asynchronously and does not block the current thread. Support for setting callback functions.

If you do not care about the result of an asynchronous operation, you can call the following function to releasewx_operation_tObject.

  • wx_operation_destroy(): Active Destructionwx_operation_tObject. The asynchronous operation continues.

If you want to cancel an asynchronous operation, you can call the following function:

  • wx_operation_cancel(): Cancels the asynchronous operation.

Please pay special attentionwx_operation_tObject's lifetime to avoid a memory leak:

  • call wx_operation_wait or wx_operation_await Function until the end of the asynchronous call, then thewx_operation_tObjects are automatically recycled.
  • If the call wx_operation_wait Timeout, you can recall wx_operation_waitFunction to continue waiting, or call thewx_operation_cancel()Actively ends this asynchronous call. cancel afterwx_operation_tObjects are also automatically recycled.
  • You can also manually callwx_operation_destroy()Destroy operation Object, in which case the asynchronous operation continues but does not receive the results of the asynchronous call.

# 1. Synchronization waiting

Synchronously blocks the current thread until the operation completes or times out.

  • If the operation is complete (including normal return and the case of an interface exception),wx_operation_t Will be automatically released.
  • If the asynchronous operation does not complete within the specified time, returns the WXERROR_TIMEOUT. The following options may be available:
    • Re-call wx_operation_wait() Keep waiting
    • call wx_operation_cancel() Cancel the asynchronous call
    • call wx_operation_destroy() Destroy operation Object.
wx_error_t wx_operation_wait(wx_operation_t operation, uint32_t timeout_ms)

parameter

attribute type Introductions
operation wx_operation_t Want to wait operation object
timeout_ms uint32_t Timeout time in milliseconds. Set to 0 Indicates no timeout.

Return value

wx_error_t wait The results of the

Error code Introductions
WXERROR_OK Interface call completes normally, operation The object is automatically released.
WXERROR_TIMEOUT operation Overtime.
Other Interface call exception returns error, operation The object is automatically released.

# 2. Asynchronous wait

Wait asynchronously and does not block the current thread. The result of an asynchronous operation will be passed through the callback Function notification.wx_operation_t The object will be released automatically.

void wx_operation_await(wx_operation_t operation,
                        wx_operation_callback_t callback,
                        void* user_Data)

parameter

attribute type Introductions
operation wx_operation_t Want to wait operation object
callback wx_operation_callback_t callback
user_Data void* The custom data you want to receive in the callback function.

# 3. Cancel asynchronous operation

Cancel the asynchronous operation,wx_operation_t The object will be released. After calling this function, you should no longer be able to wx_operation_t Object to perform any operation.

wx_error_t wx_operation_cancel(wx_operation_t operation)

parameter

attribute type Introductions
operation wx_operation_t Want to cancel. operation object

Return value

wx_error_t cancel The results of the

# 4. Release object

Active release wx_operation_t Object, but does not cancel the asynchronous operation itself. After calling this function, you should no longer be able to operation Object to perform any operation.

  • If there is no right wx_operation_t Object call wait or cancel Function, and do not care about the results of the asynchronous operation, you can call this function to manually release wx_operation_t Object.
  • When calling wx_operation_wait() The function returns WXERROR_TIMEOUT, if you do not want to wait any longer, you can call this function to release wx_operation_t Object.
  • In other cases wx_operation_t Object is automatically released and should no longer be called to actively release.
void wx_operation_destroy(wx_operation_t operation)

parameter

attribute type Introductions
operation wx_operation_t Want to release. operation object