Bạn sẽ thiết kế và kiểm chứng bounded lock-free SPSC ring buffer như thế nào, gồm trạng thái full/empty, object lifetime, wraparound và acquire-release ordering?
Xem đáp án và giải thích
Với đúng một producer và một consumer, producer sở hữu head còn consumer sở hữu tail. Một representation đơn giản dành trống một slot: head bằng tail nghĩa là empty, còn head kế tiếp bằng tail nghĩa là full, nên physical size N chứa tối đa N-1 element. Producer construct free slot trước release-store vào head; consumer acquire-load head trước khi đọc object đó, destroy nó rồi release-store tail để acquire-load của producer cho phép reuse slot an toàn. Index arithmetic phải wrap mà không dựa vào overflow assumption, đồng thời phải xử lý contract construction, move và destruction của T. Cần test ranh giới capacity, nhiều vòng wraparound, stress run dài, misuse có sanitizer hỗ trợ và xác nhận index atomic thật sự lock-free trên target. Design này không phải MPSC hay MPMC.
Với đúng một producer và một consumer, producer sở hữu head còn consumer sở hữu tail. Một representation đơn giản dành trống một slot: head bằng tail nghĩa là empty, còn head kế tiếp bằng tail nghĩa là full, nên physical size N chứa tối đa N-1 element. Producer construct free slot trước release-store vào head; consumer acquire-load head trước khi đọc object đó, destroy nó rồi release-store tail để acquire-load của producer cho phép reuse slot an toàn. Index arithmetic phải wrap mà không dựa vào overflow assumption, đồng thời phải xử lý contract construction, move và destruction của T. Cần test ranh giới capacity, nhiều vòng wraparound, stress run dài, misuse có sanitizer hỗ trợ và xác nhận index atomic thật sự lock-free trên target. Design này không phải MPSC hay MPMC.
Hãy liên hệ trực tiếp kết luận với kiểu, lifetime, ownership, overload resolution hoặc library contract xuất hiện trong câu hỏi. Nếu kết quả phụ thuộc implementation hoặc vi phạm precondition, cần nói rõ thay vì chỉ đoán output.