目录

Function Flow Runtime Job Partner (C++)

Overview

Function flow runtime (FFRT) supports the Job Partner function starting from API version 20. In some services, a thread (for example, the UI rendering thread) may take a long time, and the multi-core capability of the CPU cannot be fully utilized. In this case, you can split the large task of the original thread into multiple small tasks and assign them to other threads for execution. However, two problems may occur in practice:

To solve the preceding problems, the job partner concurrency paradigm is introduced.

In the job partner concurrency paradigm, the original thread is defined as the master thread, and partner threads can be dynamically managed. This paradigm addresses the aforementioned problems through the following two features:

Example: A Mall System

Nodes of a mall system can be created in parallel. Some code snippets created by a node must run in the JS thread, and other code snippets can run in any thread.

The original pseudocode is as follows:

namespace market_system {
    // Create nodes in batches. The API is called on the JS thread.
    void create_nodes(std::array<Node>& nodes)
    {
        for (int i = 0; i < nodes.size(); i++) {
            code1(); // Code snippet that can be executed on any thread.
            code2(); // Code snippet that must be executed on the JS thread.
            code3(); // Code snippet that can be executed on any thread.
        }
    }
};

The pseudocode after job partner parallelization is as follows:

#include <array>
#include <memory>
#include "ffrt/ffrt.h" // From the HarmonyOS third-party library "@ppd/ffrt"

namespace market_system {
    // Create nodes in batches. The API is called on the JS thread.
    void create_nodes(std::array<Node>& nodes)
    {
        constexpr uint64_t stack_size = 16 * 1024;
        auto stack = std::make_unique<std::array<char, stack_size>[]>(nodes.size()); // Create a specified number (defined by job_num) of execution stacks.
        auto partner = ffrt::job_partner<>::get_partner_of_this_thread(); // Obtain the partner of the current JS thread.
        for (int i = 0; i < nodes.size(); i++) {
            partner->submit([&] { // Submit the creation of each node to the partner.
                code1(); // Code snippet that can be executed on any thread.
                ffrt::job_partner<>::submit_to_master([&] { // Send the task that must be executed on the master thread to the main thread and wait synchronously.
                    code2(); // Code snippet that must be executed on the JS thread.
                });
                code3(); // Code snippet that can be executed on any thread.
            }, &stack[i], stack_size);
        }
        partner->wait(); // Wait until all nodes are created.
    }
};

Available APIs

The key FFRT APIs involved in the preceding example are as follows:

API Description
job_partner Fine-grained job partner API.
  • For details about how to use FFRT C++ APIs, see Using FFRT C++ APIs.
  • When using FFRT C or C++ APIs, you can use the FFRT C++ API third-party library to simplify header file inclusion, that is, use the #include "ffrt/ffrt.h" header file inclusion statement.

Constraints