Skip to content

AThread#

Represents a user-defined thread.

Header:#include <AUI/Thread/AThread.h>
CMake:aui_link(my_target PUBLIC aui::core)

Examples#

examples/ui/infinite_lazy_list/src/main.cpp

Infinite Lazy List — Usage of AUI_DECLARATIVE_FOR to make an infinite lazy list.

        auto loadFrom = state->items->size(); // base index to load from.
        state->asyncTasks << AUI_THREADPOOL {
            // perform "loading" task on a worker thread.

            AThread::sleep(500ms); // imitate hard work here

            // aka "loaded" from backend storage of some kind
            auto loadedItems = AVector<_<Item>>::generate(20, [&](size_t i) {
                return aui::ptr::manage_shared(new Item { .value = "Item {}"_format(loadFrom + i) });
            });
examples/ui/embedded_sdl/src/main.cpp

SDL3 — This code demonstrates how to integrate the AUI Framework with SDL3 to create a window with OpenGL rendering.

        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            handleSDLEvent(&event, window);
        }
        AThread::processMessages();
        /// [EventProcessing]

        /// [Rendering]
        if (window.requiresRedraw()) {
            ARenderContext render_context {

Public Types#


Interrupted#

class AThread::Interrupted

Exception that is thrown by AThread::interruptionPoint(), if interruption is requested for this thread. Handled by AThread::start.

Empty structure.

Public Methods#

asyncSleep#


static AFuture<> AThread::asyncSleep(std::chrono::milliseconds duration)

Sleep for specified duration. Most operation systems guarantee that elasped time will be greater than specified. AThread::interrupt() is supported.

Arguments
duration
sleep duration.

Async sleep.

co_await AThread::asyncSleep(1s);

current#


static const _<AAbstractThread>& AThread::current()
Returns
current thread.

Examples:

examples/7guis/cells/src/Spreadsheet.h

7GUIs Cells — Spreadsheet processor (Excel).

        struct RangeIterator {
            const Spreadsheet* spreadsheet;
            formula::Range range;
            glm::uvec2 current;

            RangeIterator operator++() {
                current.x += 1;
                if (current.x <= range.to.x) {
                    return *this;
examples/7guis/circle_drawer/src/main.cpp

7GUIs Circle Drawer — Undo, redo, dialog control.

              .name = "Adjust radius...",
              .onAction =
                  [this, circle] {
                      auto radiusPopup = _new<AWindow>(
                          "", 200_dp, 50_dp, dynamic_cast<AWindow*>(ASurface::current()), WindowStyle::MODAL);
                      radiusPopup->setContents(Vertical {
                        Label { "Adjust diameter of circle at {}."_format(circle->position) },
                        Slider {
                          .value = AUI_REACT(circle->radius / MAX_RADIUS),
                          .onValueChanged =
examples/app/game_of_life/src/main.cpp

Game of Life — Game of Life implementation that uses advanced large dynamic data rendering techniques such as ITexture, AImage to be GPU friendly. The computation is performed in AThreadPool.

    _<ITexture> mTexture;

    void updateTexture() {
        if (!mTexture) {
            mTexture = ASurface::current()->getRenderingContext()->renderer().getNewTexture();
        }

        CellsImage image(mCells->size());
        for (unsigned y = 0; y < image.height(); ++y) {
            for (unsigned x = 0; x < image.width(); ++x) {
examples/app/notes/src/main.cpp

Notes App — Note taking app that demonstrates usage of AListModel, AProperty, user data saving and loading.

public:
    using ATextArea::ATextArea;
    void onCharEntered(AChar c) override {
        if (c == '\r') {
            ASurface::current()->focusNextView();
            return;
        }
        ATextArea::onCharEntered(c);
    }
};
examples/ui/contacts/src/view/ContactDetailsView.cpp

AUI Contacts — Usage of AUI_DECLARATIVE_FOR to make a contacts-like application.

        return;
    }

    // discard
    if (AMessageBox::show(dynamic_cast<AWindow*>(ASurface::current()), "Do you really want to discard?", "This action is irreversible!", AMessageBox::Icon::NONE, AMessageBox::Button::YES_NO) != AMessageBox::ResultButton::YES) {
        return;
    }
    mContact = mOriginalContact;
    mEditorMode = false;
}
examples/ui/views/src/ExampleWindow.cpp

Views Example — All-in-one views building example.

    addView(tabView = _new<ATabView>() AUI_LET {
        it->addTab(
            AScrollArea::Builder().withContents(std::conditional_t<
                                                aui::platform::current::is_mobile(), Vertical, Horizontal> {
              Vertical {
                GroupBox {
                  Label { "CJK Font Test" },
                  Vertical {
                    Label { "Chinese: 中文测试" },

interruptionPoint#


static void AThread::interruptionPoint()

Interruption point

If the interruption flag is raised for the caller thread then flag is reset and AThread::Interrupted exception is thrown, efficiently stopping the task execution and safely freeing resources with C++'s RAII feature.

AUI_THREADPOOL, AUI_THREADPOOL_X, AThreadPool::enqueue, AUI_ENTRY handle AThread::Interrupted, so throwing AThread::Interrupted is safe.

join#


void AThread::join()

Waits for thread to be finished.

main#


static const _<AAbstractThread>& AThread::main()

Returns main thread of the application.

Examples:

processMessages#


static void AThread::processMessages()

Processes messages from other threads of current thread. Called by framework itself using IEventLoop.

Examples:

setName#


static void AThread::setName(AString name)
Arguments
name
new name of the thread

Sets name of the current thread for debugger.

sleep#


static void AThread::sleep(std::chrono::milliseconds duration)

Sleep for specified duration. Most operation systems guarantee that elasped time will be greater than specified. AThread::interrupt() is supported.

Arguments
duration
sleep duration.

Blocking sleep.

Examples:

start#


void AThread::start()

Start thread execution.

Examples:

examples/7guis/timer/src/main.cpp

7GUIs Timer — Timer example.

          } AUI_OVERRIDE_STYLE { LayoutSpacing { 4_dp } },
        });

        connect(mTimer->fired, me::update);
        mTimer->start();
    }

private:
    _<ATimer> mTimer = _new<ATimer>(100ms);
    high_resolution_clock::time_point mStartTime = high_resolution_clock::now();