Repository: microsoft/terminal
File: src/cascadia/Remoting/Monarch.cpp (C++)

Code before:
                              TraceLoggingKeyword(TIL_KEYWORD_TRACE));
        }
    }

    // Method Description:
    // - This method creates a map of peasant IDs to peasant names
    //   while removing dead peasants.
    // Arguments:
    // - <none>
    // Return Value:
    // - A map of peasant IDs to their names.
    Windows::Foundation::Collections::IVectorView<PeasantInfo> Monarch::GetPeasantInfos()
    {
        std::vector<PeasantInfo> names;
        {
            std::shared_lock lock{ _peasantsMutex };
            names.reserve(_peasants.size());
        }

        const auto func = [&](const auto& id, const auto& p) -> void {
            names.push_back({ id, p.WindowName(), p.ActiveTabTitle() });
        };

        const auto onError = [&](const auto& id) {
            TraceLoggingWrite(g_hRemotingProvider,
                              "Monarch_identifyWindows_Failed",
                              TraceLoggingInt64(id, "peasantID", "The ID of the peasant which we could not identify"),
                              TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
                              TraceLoggingKeyword(TIL_KEYWORD_TRACE));
        };

        _forEachPeasant(func, onError);

        return winrt::single_threaded_vector<PeasantInfo>(std::move(names)).GetView();
    }

    bool Monarch::DoesQuakeWindowExist()
    {
        bool result = false;
        const auto func = [&](const auto& /*id*/, const auto& p) {
            if (p.WindowName() == QuakeWindowName)
            {
                result = true;
            }
            // continue if we didn't get a positive result
            return !result;
        };

        const auto onError = [&](const auto& id) {
            TraceLoggingWrite(g_hRemotingProvider,
                              "Monarch_DoesQuakeWindowExist_Failed",

Review comment (suggestion):
BTW This makes me certain that we should create a thread safe proxy class for the Monarch in the future.
(Basically wrap the public methods of the class and make them thread-safe. The actual MonarchImpl class can then continue pretending threads don't exist.)

Code after:
        }
        catch (...)
        {
            LOG_CAUGHT_EXCEPTION();
            TraceLoggingWrite(g_hRemotingProvider,
                              "Monarch_SummonWindow_Failed",
                              TraceLoggingWideString(searchedForName.c_str(), "searchedForName", "The name of the window we tried to summon"),
                              TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
                              TraceLoggingKeyword(TIL_KEYWORD_TRACE));
        }
    }

    // Method Description:
    // - This method creates a map of peasant IDs to peasant names
    //   while removing dead peasants.
    // Arguments:
    // - <none>
    // Return Value:
    // - A map of peasant IDs to their names.
    Windows::Foundation::Collections::IVectorView<PeasantInfo> Monarch::GetPeasantInfos()
    {
        std::vector<PeasantInfo> names;
        {
            std::shared_lock lock{ _peasantsMutex };
            names.reserve(_peasants.size());
        }

        const auto func = [&](const auto& id, const auto& p) -> void {
            names.push_back({ id, p.WindowName(), p.ActiveTabTitle() });
        };

        const auto onError = [&](const auto& id) {
            TraceLoggingWrite(g_hRemotingProvider,
                              "Monarch_identifyWindows_Failed",
                              TraceLoggingInt64(id, "peasantID", "The ID of the peasant which we could not identify"),
                              TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
                              TraceLoggingKeyword(TIL_KEYWORD_TRACE));
        };

        _forEachPeasant(func, onError);

        return winrt::single_threaded_vector<PeasantInfo>(std::move(names)).GetView();
    }

    bool Monarch::DoesQuakeWindowExist()
    {
        bool result = false;
        const auto func = [&](const auto& /*id*/, const auto& p) {
            if (p.WindowName() == QuakeWindowName)
            {
                result = true;