返回题库

闩锁屏障启动

Latch Barrier Startup

专题
Systems & Architecture / 系统与架构
难度
L2
来源
MyntBit

题目详情

高频交易系统依赖多个关键子系统——行情源、订单管理和风控——必须在主交易循环开始前完成初始化。现代实现通常使用 latch 同步来确保依赖组件等待其前置条件就绪,形成初始化阶段的 DAG。

任务:实现 StartupLatch 类,模拟多阶段启动流程。每个子系统通过 latch.wait() 等待其依赖子系统完成初始化,初始化完成后通过 latch.count_down() 通知下游。实现 DAG 式的并行启动协调。

英文原题

High-frequency trading systems rely on multiple critical subsystems—such as market data feeds, order management, and risk controls—that must be fully initialized before the main trading loop begins. Modern implementations often use latch-based synchronization to ensure dependent components wait for their prerequisites, forming a directed acyclic graph of initialization phases. Simulating and optimizing this startup sequence is crucial for minimizing system downtime and reducing time-to-market la

解析

问题分析

std::latch (C++20) 是一次性倒计数器。主线程在启动所有 worker 后等待 latch 到达 0,worker 线程完成初始化后递减 latch。适合系统启动时确保所有组件就绪。

实现

class TradingSystem {
    std::latch startup_latch_{3};  // 3 个组件
    void startMarketData() { connect(); startup_latch_.count_down(); }
    void startOrderGateway() { login(); startup_latch_.count_down(); }
    void startRiskEngine() { loadLimits(); startup_latch_.count_down(); }
public:
    void run() {
        std::thread(&TradingSystem::startMarketData, this).detach();
        std::thread(&TradingSystem::startOrderGateway, this).detach();
        std::thread(&TradingSystem::startRiskEngine, this).detach();
        startup_latch_.wait();  // 阻塞直到全部就绪
    }
};

复杂度与边界

  • 时间复杂度:count_down O(1),wait 取决于最慢组件
  • 空间复杂度:O(1)
  • 边界条件:(1) count_down 比 latch 总数多会导致未定义行为 (2) latch 不可重用——使用 barrier 替代

英文解析

Analysis

std::latch (C++20) is a one-shot countdown barrier. The main thread waits on the latch to reach zero after starting all workers, while each worker thread decrements the latch after completing initialization. This ensures all components are ready before the system begins processing.

Solution

class TradingSystem {
    std::latch startup_latch_{3};
    void startMarketData() { connect(); startup_latch_.count_down(); }
    void startOrderGateway() { login(); startup_latch_.count_down(); }
    void startRiskEngine() { loadLimits(); startup_latch_.count_down(); }
public:
    void run() {
        std::thread(&TradingSystem::startMarketData, this).detach();
        std::thread(&TradingSystem::startOrderGateway, this).detach();
        std::thread(&TradingSystem::startRiskEngine, this).detach();
        startup_latch_.wait();
    }
};

Complexity & Edge Cases

  • Time complexity: count_down O(1), wait depends on slowest component
  • Space complexity: O(1)
  • Edge cases: (1) Excess count_down beyond latch total causes undefined behavior (2) Latch is non-reusable - use barrier for reusable synchronization

Verification

Verify all three components initialize before run() proceeds. Test that excess count_down triggers expected behavior. Confirm wait blocks until all workers signal ready.

Key Considerations

std::latch provides a cleaner startup synchronization pattern than manual condition variables. In trading system initialization, ensuring all subsystems (market data, order gateway, risk engine) are ready before accepting orders prevents cascading failures on startup.