folly::ProducerConsumerQueue — SPSC 큐 분석
#한 줄 요약
folly::ProducerConsumerQueue<T>는 한 명의 producer와 한 명의 consumer만을 가정한 SPSC(single-producer single-consumer) lock-free ring buffer다. 락이 없고 CAS도 없다. write/read 인덱스를 각각의 스레드만 갱신하며, 두 인덱스를 서로 다른 캐시 라인에 둬서 false sharing을 막는다.
#동기 — 왜 SPSC인가
MPMC(multi-producer multi-consumer) 큐는 보편적이지만 비싸다. 매 push가 CAS이고, 모든 producer가 같은 tail을 두고 경쟁한다. 그러나 실제 시스템에서 “producer 1명, consumer 1명” 패턴은 굉장히 자주 나타난다.
- 네트워크 RX 인터럽트 스레드 → 워커 스레드
- 메인 스레드 → 렌더링 스레드 (frame queue)
- 오디오 콜백 → 오디오 디코더
- IOThreadPool의 한 EventBase → 다음 단계 큐
이런 경우 MPMC를 쓰는 것은 낭비다. SPSC를 쓰면 CAS 없이 단순 load/store + memory barrier만으로 충분하다. Folly의 측정으로 동일 메시지 크기 기준 MPMC 대비 RTT가 3-5배 줄어든다.
#Producer/Consumer 일반 패턴
SPSC, MPSC, MPMC 모두 이 일반 형태의 변종이다.
생산자가 큐에 enqueue, 소비자가 dequeue. capacity가 차면 backpressure — 생산자가 block / fail / drop. 변종은 producer/consumer 수와 blocking 정책(blocking vs lock-free, bounded vs unbounded)으로 갈린다.
#API
#include <folly/ProducerConsumerQueue.h>
folly::ProducerConsumerQueue<int> q(1024); // 용량은 생성 시 고정
// Producer 스레드if (!q.write(42)) { // queue가 가득 참 (non-blocking)}
// Consumer 스레드int v;if (q.read(v)) { // v에 값이 들어옴}핵심 제약은 세 가지다.
- 용량은 생성자에서 고정. 동적 확장은 없다.
- 단일 producer, 단일 consumer. 두 명 이상이 동시에 write/read를 호출하면 UB.
- non-blocking. queue가 가득/비어 있으면 write/read가 즉시 false를 돌려준다. busy wait은 호출자가 처리한다.
#내부 구현 — read/write index의 분리
template <class T>struct ProducerConsumerQueue { const uint32_t size_; T* const records_;
alignas(folly::cacheline_align_v) std::atomic<unsigned int> readIndex_; alignas(folly::cacheline_align_v) std::atomic<unsigned int> writeIndex_;};readIndex_는 consumer만 store, producer는 load만 한다.writeIndex_는 producer만 store, consumer는 load만 한다.- 두 인덱스 사이에
alignas(cacheline)를 박아 false sharing을 차단한다.
false sharing이 왜 문제인가. 두 인덱스가 같은 캐시 라인(64B)에 있으면, producer가 writeIndex_를 갱신할 때 consumer 코어의 readIndex_ 캐시 라인이 무효화된다. read는 캐시에서 가져올 수 있는 값을 매번 메모리에서 다시 가져온다. cacheline_align_v 하나로 RTT가 절반이 된다.
#write 구현
template <class... Args>bool write(Args&&... recordArgs) noexcept { auto const currentWrite = writeIndex_.load(std::memory_order_relaxed); auto nextRecord = currentWrite + 1; if (nextRecord == size_) nextRecord = 0;
if (nextRecord != readIndex_.load(std::memory_order_acquire)) { new (&records_[currentWrite]) T(std::forward<Args>(recordArgs)...); writeIndex_.store(nextRecord, std::memory_order_release); return true; } return false; // full}핵심은 메모리 순서다.
readIndex_load는 acquire — consumer가 끝까지 읽은 슬롯의 가시성을 보장.writeIndex_store는 release — 위 placement-new가 consumer에게 보이도록 publish.
이 두 fence만으로 happens-before가 성립한다. mutex도 CAS도 필요 없다.
#read 구현
bool read(T& record) noexcept { auto const currentRead = readIndex_.load(std::memory_order_relaxed); if (currentRead == writeIndex_.load(std::memory_order_acquire)) { return false; // empty }
auto nextRecord = currentRead + 1; if (nextRecord == size_) nextRecord = 0;
record = std::move(records_[currentRead]); records_[currentRead].~T(); readIndex_.store(nextRecord, std::memory_order_release); return true;}대칭적이다. writeIndex_ acquire로 producer의 publish를 보고, readIndex_ release로 슬롯이 비었음을 producer에게 알린다.
#실용 크기 — size_ - 1
생성자가 size_ = capacity + 1로 잡는 이유가 있다. ring buffer에서 “가득 참”과 “비었음”을 구분하려면 한 슬롯을 비워둬야 한다. 1024개를 담고 싶다면 1025로 잡힌다.
#std / abseil 비교
| 큐 | 동시성 모델 | 락 | 동적 크기 | 비고 |
|---|---|---|---|---|
std::queue + mutex | MPMC | yes | yes | 가장 단순. 대부분 충분 |
boost::lockfree::spsc_queue | SPSC | no | no | 거의 동일한 설계 |
folly::ProducerConsumerQueue | SPSC | no | no | cacheline 패딩이 명시적 |
folly::MPMCQueue | MPMC | no | no | ticket-based, 다음 절 |
표준 라이브러리에는 SPSC 전용이 없다. boost::lockfree가 가장 가까운데, Folly와 거의 같은 구조다. 차이는 Folly가 jemalloc·cache-line 정렬·placement-new 같은 production 디테일을 더 노골적으로 드러낸다는 점이다.
#코드 리뷰 포인트
#1. capacity는 정말 충분한가
write가 false를 돌려주면 호출자가 처리해야 한다. 보통 두 가지 선택이다.
// A) drop (실시간 시스템에서 자주 쓴다)if (!q.write(sample)) { ++drops_;}
// B) backoffwhile (!q.write(sample)) { std::this_thread::yield();}backoff 패턴이 잦다면 capacity가 부족한 신호다. 큐 사용량을 측정하고 P99 기준으로 잡는다.
#2. T의 소멸자 비용
read는 records_[currentRead].~T()로 명시 소멸을 호출한다. T가 무거우면(예: std::string을 가진 struct) consumer 핫 패스에서 비용이 발생한다. 가능하면 작은 POD나 unique_ptr<Heavy>로 감싸 소멸 비용을 producer 측으로 옮긴다.
#3. emplace 대신 write의 가변 인자
q.write(arg1, arg2, arg3); // T를 in-place 생성write(T&&)만 있는 게 아니다. write(Args&&...)이 placement-new로 in-place 생성한다. 큰 객체를 큐에 넣을 때 이동·복사 비용을 한 번 더 줄인다.
#4. producer/consumer 동일성 검증
런타임에 single-producer/single-consumer 제약을 강제하지 않는다. 테스트에서 두 명 이상이 write를 호출하면 UB가 조용히 발생한다. PR 리뷰 시 “이 큐에 write는 어느 스레드만 하는가” 명시 주석을 요구한다.
// 회피folly::ProducerConsumerQueue<Frame> q(1024);// 누가 write 하는지 코드만 봐서는 모름
// Good// PRODUCER: AudioCallbackThread only// CONSUMER: DecoderThread onlyfolly::ProducerConsumerQueue<Frame> q(1024);#안티패턴
#1. wait 루프에서 sleep 없이 spin
// 회피int v;while (!q.read(v)) {} // 100% CPUempty 상태에서 read가 false면 코어가 통째로 타버린다. backoff 전략이 필요하다.
// Goodint v;while (!q.read(v)) { std::this_thread::yield(); // 또는 pause/sleep}
// Better — Baton/EventFd로 wakeup진짜 busy wait이 필요한 워크로드(예: 오디오 callback)는 spin이 의도된 것이고, 그 외에는 wakeup 메커니즘을 함께 둔다.
#2. T가 noexcept move를 보장하지 않음
read가 record = std::move(records_[currentRead]);를 한다. T의 move가 throw하면 큐 상태가 망가진다. T는 noexcept-movable이어야 한다.
static_assert(std::is_nothrow_move_assignable_v<T>);#3. ProducerConsumerQueue를 여러 producer에 공유
// 회피 — UBfolly::ProducerConsumerQueue<int> q(1024);std::thread p1([&]{ q.write(1); });std::thread p2([&]{ q.write(2); }); // UB이 경우는 다음 절의 MPMCQueue를 써야 한다.
#정리
- ProducerConsumerQueue는 SPSC 전용 lock-free ring buffer다.
- read/write index를 서로 다른 캐시 라인에 둬서 false sharing을 막는다.
- 메모리 순서는 producer release / consumer acquire 한 쌍이면 충분하다.
- capacity 고정, non-blocking, T는 noexcept-movable이어야 한다.
- “한 producer, 한 consumer” 패턴이 정확히 맞으면 MPMC보다 3-5배 빠르다.
- 한 명이 더 추가되는 순간 UB이므로 코드 주석으로 명시한다.
#다음 편
Part 10-02 MPMCQueue — multi-producer multi-consumer 버전. ticket-based 알고리즘으로 어떻게 락 없이 다자간 동시 접근을 처리하는지 본다.
#관련 항목
- Part 9-01 Synchronized — 데이터에 락을 묶는 일반 패턴
- Part 3-05 EventBase — EventBase 간 메시지 전달에 SPSC 자주 사용
- Effective Modern C++ Item 41 — move semantics와 noexcept
Folly Code Review · 46 of 89
- 1 Folly Code Review — Meta의 production-grade C++ 라이브러리 코드 분석
- 2 Folly 개요 — Meta가 production에서 검증한 utility 모음 분석
- 3 Folly vs Abseil 철학 비교 — performance-first vs std-compatible
- 4 Folly 빌드와 fbcode 환경 — monorepo의 그림자
- 5 Folly API stability 정책 — 어떤 보장도 없다는 솔직함
- 6 Folly production validation 문화 — peta-scale에서 단련된 코드
- 7 folly::Future 분석 — std::future의 한계를 넘는 composable async
- 8 folly::Promise·makeFuture — Future를 만드는 두 길
- 9 folly::SemiFuture vs Future — executor binding의 명시화
- 10 folly::Future thenValue·thenError·thenTry — continuation 체인 분석
- 11 folly::collect·collectAll·collectAny — fan-in 패턴 분석
- 12 folly::Future retry·window·via — 제어 흐름 조합자
- 13 folly::fibers 분석 — M:N stackful coroutine
- 14 folly::InlineExecutor — 호출자 thread에서 즉시 실행
- 15 folly::CPUThreadPoolExecutor — CPU-bound 작업의 표준 thread pool
- 16 folly::IOThreadPoolExecutor — libevent 기반 I/O pool
- 17 folly::ManualExecutor — 결정적 테스트를 위한 수동 진행
- 18 folly::EventBase 분석 — libevent 이벤트 루프의 핵심
- 19 folly::IOBuf 분석 — zero-copy buffer chain의 기본 단위
- 20 folly::IOBufQueue — chain의 push/pull 추상화
- 21 folly::io::Cursor·RWCursor — chain 위의 stream
- 22 folly Zero-copy 패턴 — IOBuf로 ScatterGather I/O 표현
- 23 folly::IOBuf shared semantics — clone·unshare·takeOwnership
- 24 folly::FBString 분석 — SSO + COW 구현
- 25 folly의 fmt::format 통합 — 모던 포맷팅 채택
- 26 folly::StringPiece — string_view 호환 분석
- 27 folly Join·Split utilities — 문자열 분해와 결합
- 28 folly::to·tryTo — text↔num 변환 분석
- 29 folly Conv Customization — 사용자 타입 지원
- 30 folly Conv 성능 비교 — sprintf·stringstream 대비
- 31 folly::F14ValueMap vs std::unordered_map
- 32 folly::F14NodeMap — stable pointer가 필요할 때
- 33 folly::F14VectorMap — cache-friendly iteration
- 34 folly::F14FastMap — auto-select 동작
- 35 folly F14 internals — SIMD probing 메커니즘
- 36 folly::small_vector — inline storage 분석
- 37 folly::FixedString — compile-time string
- 38 folly::AtomicHashMap — lock-free read 분석
- 39 folly::ConcurrentHashMap — sharded 동시 해시 맵
- 40 folly::EvictingCacheMap — LRU 구현 분석
- 41 folly::Synchronized — lock wrapper 패턴
- 42 folly::SharedMutex 분석
- 43 folly::Baton — one-shot wait 동기화
- 44 folly::RWSpinLock 분석
- 45 folly::PicoSpinLock — 1-byte spinlock
- 46 folly::ProducerConsumerQueue — SPSC 큐 분석
- 47 folly::MPMCQueue — multi-producer multi-consumer
- 48 folly::UnboundedQueue — 동적 크기 lock-free
- 49 folly::fibers::Channel — Go-like channel
- 50 folly::dynamic — JSON-like dynamic type 분석
- 51 folly JSON conversion — toJson·parseJson
- 52 folly dynamic ↔ struct — manual marshaling
- 53 folly dynamic Visitor pattern — type별 분기
- 54 folly::Singleton vs Meyers/static — 왜 Folly의 Singleton인가
- 55 folly::SingletonVault 분석 — 등록·소멸·의존성
- 56 folly::Singleton try_get·try_get_fast — TLS-cached 접근
- 57 folly::ExceptionWrapper — type-erased exception holder
- 58 folly::ScopeGuard·SCOPE_EXIT — RAII cleanup
- 59 folly::Optional vs std::optional
- 60 folly::Function vs std::function
- 61 folly::Lazy — 지연 초기화 wrapper
- 62 folly Meta 스타일 code review 패턴
- 63 folly anti-patterns — 잘못 쓰면 std보다 느림
- 64 folly vs std 선택 기준 분석
- 65 folly::coro 개요 — production C++20 코루틴 어댑터
- 66 folly::coro::Task — lazy single-shot 코루틴
- 67 folly::coro::AsyncGenerator — 비동기 스트림
- 68 folly coro blockingWait·collectAll — 동기 경계와 fan-in
- 69 folly::coro::Baton·Mutex — 코루틴-aware 동기화
- 70 folly::Expected — 결과 또는 오류
- 71 folly::Try — Future 결과 wrapper
- 72 folly::Try vs Expected 선택 기준
- 73 folly::Range — 일반 iterator pair
- 74 folly::Uri — URL 파서
- 75 folly Fingerprint64·128 — 분산 hash
- 76 folly SpookyHashV2 — fast non-crypto hash
- 77 folly::Init — main() 부트스트랩
- 78 folly::Indestructible — global lifetime 패턴
- 79 folly::MicroLock — 1-byte 락
- 80 folly::MicroSpinLock — 가장 좁은 spin lock
- 81 folly::format — legacy formatter 분석
- 82 folly::demangle — typeid 디망글링
- 83 folly::DynamicConverter — dynamic ↔ struct
- 84 folly::RecordIO — append-only 로그 파일 포맷
- 85 folly::io::Compression — zstd·lz4·snappy wrapper
- 86 folly::AsyncIO — io_uring·Linux AIO
- 87 folly::CancellationToken — 코루틴·Future 취소 전파
- 88 folly::observer — hot config의 atomic refresh
- 89 fbcode 패턴 모음 — folly 사용의 실전
관련 글
folly::UnboundedQueue — 동적 크기 lock-free
Part 10-03: UnboundedQueue — linked segment 기반 동적 크기 lock-free 큐. SPSC 모드에선 거의 무비용으로 성장한다.
같은 시리즈에서 이어 읽기
folly::MPMCQueue — multi-producer multi-consumer
Part 10-02: MPMCQueue — ticket 기반 lock-free 큐. CAS 없이 여러 producer/consumer를 안전하게 처리한다.
같은 시리즈에서 이어 읽기
folly::AtomicHashMap — lock-free read 분석
AtomicHashMap — lock-free read, append-only insert, 큰 read-heavy 워크로드용 hash map.
같은 시리즈에서 이어 읽기