{"id":86344,"date":"2026-07-28T13:32:36","date_gmt":"2026-07-28T05:32:36","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/86344.html"},"modified":"2026-07-28T13:32:36","modified_gmt":"2026-07-28T05:32:36","slug":"%e4%bb%8e-gcc-13-%e5%8f%8d%e6%b1%87%e7%bc%96%e7%9c%8b%e5%86%85%e5%ad%98%e4%b8%80%e8%87%b4%e6%80%a7%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88%e6%99%ae%e9%80%9a%e7%9a%84-mov-%e5%9c%a8-arm-%e4%b8%8a%e5%bf%85","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/86344.html","title":{"rendered":"\u4ece GCC 13 \u53cd\u6c47\u7f16\u770b\u5185\u5b58\u4e00\u81f4\u6027\uff1a\u4e3a\u4ec0\u4e48\u666e\u901a\u7684 MOV \u5728 ARM \u4e0a\u5fc5\u987b\u53d8\u6210 LDAR\/STLR\uff1f"},"content":{"rendered":"<p>\u5927\u91cf GitHub \u4e0a\u7684\u65e0\u9501\u961f\u5217\u5b9e\u73b0\u4f9d\u8d56 x86-TSO \u7684\u5f3a\u5185\u5b58\u6a21\u578b\u201c\u78b0\u5de7\u6b63\u786e\u201d&#xff0c;\u8fc1\u79fb\u5230 ARM&#xff08;\u5305\u62ec Apple Silicon\u3001AWS Graviton&#xff09;\u540e\u7acb\u523b\u66b4\u9732\u7f3a\u5931\u7684 acquire\/release \u5c4f\u969c&#xff0c;\u8868\u73b0\u4e3a\u95f4\u6b47\u6027\u8bfb\u5230\u810f\u6570\u636e\u3002\u672c\u6587\u4ece\u4e00\u6bb5 GCC 13 \u5728 x86-64 \u4e0e AArch64 \u4e0a\u7684\u5bf9\u7167\u53cd\u6c47\u7f16\u51fa\u53d1&#xff0c;\u62c6\u6e05 memory_order_relaxed\/acquire\/release\/seq_cst \u5728\u6536\u53d1\u7ebf\u7a0b\u6a21\u578b\u4e2d\u7684\u6b63\u786e\u6620\u5c04&#xff0c;\u8d70\u8bfb Folly \u548c rigtorp \u7684\u771f\u5b9e\u4ee3\u7801\u770b cache line padding \u5982\u4f55\u6d88\u706d false sharing&#xff0c;\u5256\u6790\u7f51\u7edc buffer \u590d\u7528\u573a\u666f\u4e0b ABA \u4e0e epoch-based reclamation \u7684\u751f\u547d\u5468\u671f\u9677\u9631&#xff0c;\u8bba\u8bc1\u4e3a\u4ec0\u4e48\u201c\u4e00\u4e2a IO \u7ebf\u7a0b\u5582\u591a\u4e2a worker\u201d\u5e94\u8be5\u7528 MPSC \u800c\u975e MPMC&#xff0c;\u5e76\u7ed9\u51fa\u4e00\u4e2a\u53ef\u79fb\u690d SPSC ring \u7684\u5b8c\u6574\u5b9e\u73b0\u3002<\/p>\n<hr \/>\n<h3>\u4e00\u6bb5\u53cd\u6c47\u7f16\u63ed\u5f00\u7684\u771f\u76f8<\/h3>\n<p>\u770b\u4e24\u6bb5\u4e00\u6a21\u4e00\u6837\u7684 C&#043;&#043; \u4ee3\u7801&#xff0c;GCC 13 -O2 \u5206\u522b\u4e3a x86-64 \u548c AArch64 \u751f\u6210\u4e86\u4ec0\u4e48&#xff08;\u53ef\u5728 Compiler Explorer \u590d\u9a8c&#xff09;&#xff1a;<\/p>\n<p>C&#043;&#043;<\/p>\n<p>\/\/ C&#043;&#043;20, GCC 13, -O2<br \/>\n#include &lt;atomic&gt;<\/p>\n<p>std::atomic&lt;int&gt; flag{0};<br \/>\nint payload &#061; 0;<\/p>\n<p>\/\/ \u751f\u4ea7\u8005<br \/>\nvoid producer() {<br \/>\n    payload &#061; 42;<br \/>\n    flag.store(1, std::memory_order_release);<br \/>\n}<\/p>\n<p>\/\/ \u6d88\u8d39\u8005<br \/>\nint consumer() {<br \/>\n    while (flag.load(std::memory_order_acquire) &#061;&#061; 0) {}<br \/>\n    return payload;<br \/>\n}<\/p>\n<p>x86-64 \u7684\u8f93\u51fa&#xff1a;<\/p>\n<p>text<\/p>\n<p>producer():<br \/>\n    mov     DWORD PTR payload[rip], 42<br \/>\n    mov     DWORD PTR flag[rip], 1     ; \u5c31\u662f\u4e00\u6761\u666e\u901a MOV<br \/>\n    ret<\/p>\n<p>consumer():<br \/>\n.L2:<br \/>\n    mov     eax, DWORD PTR flag[rip]   ; \u4e5f\u662f\u4e00\u6761\u666e\u901a MOV<br \/>\n    test    eax, eax<br \/>\n    je      .L2<br \/>\n    mov     eax, DWORD PTR payload[rip]<br \/>\n    ret<\/p>\n<p>AArch64 \u7684\u8f93\u51fa&#xff1a;<\/p>\n<p>text<\/p>\n<p>producer():<br \/>\n    adrp    x8, payload<br \/>\n    mov     w9, #42<br \/>\n    str     w9, [x8, :lo12:payload]<br \/>\n    adrp    x8, flag<br \/>\n    stlr    w9, [x8, :lo12:flag]       ; STLR &#061; Store-Release<br \/>\n    ret<\/p>\n<p>consumer():<br \/>\n.L2:<br \/>\n    adrp    x8, flag<br \/>\n    ldar    w0, [x8, :lo12:flag]       ; LDAR &#061; Load-Acquire<br \/>\n    cbz     w0, .L2<br \/>\n    adrp    x8, payload<br \/>\n    ldr     w0, [x8, :lo12:payload]<br \/>\n    ret<\/p>\n<p>\u5dee\u5f02\u4e00\u76ee\u4e86\u7136\u3002x86-64 \u4e0a\u00a0memory_order_acquire\u00a0\u548c\u00a0memory_order_release\u00a0\u7f16\u8bd1\u51fa\u6765\u8ddf\u00a0memory_order_relaxed\u00a0\u4e00\u6a21\u4e00\u6837\u2014\u2014\u90fd\u662f\u666e\u901a\u00a0MOV\u3002ARM \u4e0a\u5219\u5fc5\u987b\u7528\u00a0LDAR&#xff08;Load-Acquire&#xff09;\u548c\u00a0STLR&#xff08;Store-Release&#xff09;\u8fd9\u4e24\u6761\u4e13\u95e8\u7684\u5355\u5411\u5c4f\u969c\u6307\u4ee4\u3002<\/p>\n<p>\u8fd9\u5c31\u662f\u95ee\u9898\u7684\u6839\u6e90&#xff1a;\u5982\u679c\u4f60\u628a release \u5199\u6210\u4e86 relaxed&#xff0c;x86 \u4e0a\u7f16\u8bd1\u51fa\u6765\u7684\u673a\u5668\u7801\u5b8c\u5168\u76f8\u540c&#xff0c;\u8dd1\u4e00\u5343\u4e07\u6b21\u4e5f\u6d4b\u4e0d\u51fa bug\u3002\u4f46 ARM \u4e0a\u00a0STR&#xff08;\u666e\u901a\u5199&#xff09;\u548c\u00a0STLR&#xff08;Store-Release&#xff09;\u662f\u4e24\u6761\u622a\u7136\u4e0d\u540c\u7684\u6307\u4ee4&#xff0c;\u524d\u8005\u4e0d\u963b\u6b62\u540e\u7eed\u8bfb\u5199\u88ab CPU \u91cd\u6392\u5230\u5b83\u524d\u9762&#xff0c;\u6d88\u8d39\u8005\u7ebf\u7a0b\u770b\u5230\u00a0flag &#061;&#061; 1\u00a0\u65f6\u00a0payload\u00a0\u53ef\u80fd\u8fd8\u662f\u5783\u573e\u503c\u3002<\/p>\n<p>\u8fd9\u4e0d\u662f\u7406\u8bba\u95ee\u9898\u3002\u6211\u7ecf\u624b\u8fc7\u4e00\u4e2a\u6d88\u606f\u4e2d\u95f4\u4ef6\u7684 ARM \u79fb\u690d&#xff0c;\u539f\u6765\u5728 x86 \u670d\u52a1\u5668\u4e0a\u7a33\u5b9a\u8fd0\u884c\u4e24\u5e74\u7684\u65e0\u9501\u961f\u5217&#xff0c;\u4e0a Graviton3 \u540e\u5927\u7ea6\u6bcf\u767e\u4e07\u6b21\u5165\u961f\u51fa\u4e00\u6b21\u6570\u636e\u9519\u4e71\u2014\u2014\u9519\u4e71\u7684\u4e0d\u662f\u961f\u5217\u7ed3\u6784\u672c\u8eab&#xff0c;\u662f\u6d88\u8d39\u8005\u8bfb\u5230\u4e86\u751f\u4ea7\u8005\u8fd8\u6ca1\u5199\u5b8c\u7684 payload\u3002\u6392\u67e5\u4e86\u4e09\u5929\u624d\u5b9a\u4f4d\u5230\u4e00\u4e2a\u00a0memory_order_relaxed\u00a0\u5e94\u8be5\u662f\u00a0memory_order_release\u00a0\u7684 store\u3002\u5728 x86 \u4e0a&#xff0c;\u8fd9\u4e24\u8005\u751f\u6210\u7684\u6307\u4ee4**\u5b8c\u5168\u76f8\u540c**\u3002<\/p>\n<p>\u8fd9\u7bc7\u6587\u7ae0\u8981\u56de\u7b54\u4e00\u4e2a\u5224\u65ad&#xff1a;\u5927\u591a\u6570\u201c\u65e0\u9501\u961f\u5217\u201d\u5b9e\u73b0\u7684\u6b63\u786e\u6027\u662f x86-TSO \u8fd9\u4e2a\u786c\u4ef6\u7279\u6027\u7684\u526f\u4ea7\u54c1&#xff0c;\u4e0d\u662f\u8bbe\u8ba1\u8005\u6709\u610f\u4e3a\u4e4b&#xff1b;\u800c\u4e14\u591a\u6570\u7f51\u7edc\u573a\u666f\u6839\u672c\u4e0d\u9700\u8981 MPMC\u2014\u2014\u7528 SPSC\/MPSC \u66f4\u5feb\u3001\u66f4\u5bb9\u6613\u8bc1\u660e\u6b63\u786e\u3002<\/p>\n<hr \/>\n<h3>x86-TSO \u5230\u5e95\u4fdd\u8bc1\u4e86\u4ec0\u4e48&#xff0c;\u53c8\u6ca1\u4fdd\u8bc1\u4ec0\u4e48<\/h3>\n<p>\u8981\u7406\u89e3\u4e3a\u4ec0\u4e48 x86 \u4e0a\u6d4b\u4e0d\u51fa bug&#xff0c;\u5f97\u5148\u7cbe\u786e\u7406\u89e3 x86 \u7684 Total Store Order&#xff08;TSO&#xff09;\u6a21\u578b\u5230\u5e95\u627f\u8bfa\u4e86\u4ec0\u4e48\u3002\u4e0d\u662f\u201c\u5f3a\u4e00\u81f4\u201d\u2014\u2014\u8fd9\u4e2a\u8bcd\u592a\u7b3c\u7edf&#xff0c;\u5bb9\u6613\u4ea7\u751f\u865a\u5047\u5b89\u5168\u611f\u3002<\/p>\n<p>Intel \u7684 Software Developer Manual Volume 3A \u00a79.2 \u7ed9\u51fa\u7684 TSO \u4fdd\u8bc1\u53ef\u4ee5\u7cbe\u7b80\u4e3a\u56db\u6761\u89c4\u5219&#xff1a;<\/p>\n<li>\n<p>Store \u4e0d\u4e0e Store \u91cd\u6392\u3002\u5982\u679c\u4e00\u4e2a\u6838\u5148\u5199\u4e86 A \u518d\u5199\u4e86 B&#xff0c;\u5176\u4ed6\u6838\u770b\u5230 B \u7684\u65b0\u503c\u65f6\u4e00\u5b9a\u4e5f\u80fd\u770b\u5230 A \u7684\u65b0\u503c\u3002<\/p>\n<\/li>\n<li>\n<p>Load \u4e0d\u4e0e Load \u91cd\u6392\u3002\u5982\u679c\u4e00\u4e2a\u6838\u5148\u8bfb\u4e86 X \u518d\u8bfb\u4e86 Y&#xff0c;\u5b83\u8bfb\u5230\u7684 X \u503c\u4e0d\u4f1a\u6bd4\u8bfb\u5230\u7684 Y \u503c\u201c\u66f4\u65b0\u201d\u3002<\/p>\n<\/li>\n<li>\n<p>Load \u4e0d\u4e0e\u5148\u524d\u7684 Store \u91cd\u6392\u2014\u2014\u4f46\u6709\u4e00\u4e2a\u5173\u952e\u4f8b\u5916&#xff1a;Store \u53ef\u4ee5\u88ab\u540e\u7eed\u7684 Load \u201c\u8d8a\u8fc7\u201d\u3002\u4e5f\u5c31\u662f\u8bf4&#xff0c;\u4e00\u4e2a\u6838\u5199\u4e86 A \u4e4b\u540e\u8bfb B&#xff0c;\u53ef\u80fd\u5728\u5199 A \u8fd8\u6ca1\u5bf9\u5916\u53ef\u89c1\u65f6\u5c31\u5148\u770b\u5230\u4e86 B \u7684\u503c\u3002<\/p>\n<\/li>\n<li>\n<p>\u6240\u6709\u6838\u89c2\u5bdf\u5230\u7684 Store \u987a\u5e8f\u4e00\u81f4\u3002\u4e0d\u5b58\u5728\u6838 0 \u770b\u5230\u201c\u5148\u5199 A \u518d\u5199 B\u201d\u800c\u6838 1 \u770b\u5230\u201c\u5148\u5199 B \u518d\u5199 A\u201d\u7684\u60c5\u51b5\u3002<\/p>\n<\/li>\n<p>\u8fd9\u56db\u6761\u89c4\u5219\u7684\u5de5\u7a0b\u542b\u4e49\u662f&#xff1a;**\u5728 x86 \u4e0a&#xff0c;\u6bcf\u4e00\u6b21\u666e\u901a\u7684\u00a0MOV\u00a0store \u81ea\u5e26 release \u8bed\u4e49&#xff0c;\u6bcf\u4e00\u6b21\u666e\u901a\u7684\u00a0MOV\u00a0load \u81ea\u5e26 acquire \u8bed\u4e49\u3002** \u7f16\u8bd1\u5668\u4e3a\u00a0memory_order_acquire\u00a0\u548c\u00a0memory_order_release\u00a0\u53ea\u9700\u8981\u63d2\u4e00\u4e2a\u7f16\u8bd1\u5668\u5c4f\u969c&#xff08;asm volatile(&#034;&#034; ::: &#034;memory\u201c)&#xff09;&#xff0c;\u544a\u8bc9\u4f18\u5316\u5668\u522b\u628a\u524d\u540e\u7684\u5185\u5b58\u8bbf\u95ee\u8de8\u8fc7\u8fd9\u4e2a\u70b9\u91cd\u6392&#xff0c;CPU \u786c\u4ef6\u5df2\u7ecf\u4fdd\u8bc1\u4e86\u5269\u4e0b\u7684\u4e8b\u3002<\/p>\n<p>\u552f\u4e00\u9700\u8981\u771f\u6b63\u786c\u4ef6\u5c4f\u969c\u7684\u662f\u00a0memory_order_seq_cst\u00a0\u7684 store\u2014\u2014\u5b83\u9700\u8981\u4e00\u6761\u00a0MFENCE&#xff08;\u6216\u8005\u7528\u00a0LOCK XCHG&#xff09;\u6765\u963b\u6b62\u89c4\u5219 3 \u4e2d\u90a3\u4e2a\u201dStore \u88ab\u540e\u7eed Load \u8d8a\u8fc7&#034;\u7684\u91cd\u6392\u3002\u8fd9\u662f TSO \u552f\u4e00\u5141\u8bb8\u7684\u91cd\u6392\u65b9\u5411\u3002<\/p>\n<p>\u753b\u6210\u4e00\u5f20\u8868&#xff1a;<\/p>\n<p>text<\/p>\n<p>            x86-64 \u751f\u6210\u7684\u6307\u4ee4                ARM AArch64 \u751f\u6210\u7684\u6307\u4ee4<br \/>\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\nrelaxed     MOV                              LDR \/ STR<br \/>\nacquire     MOV  &#xff08;&#043; \u7f16\u8bd1\u5668\u5c4f\u969c&#xff09;             LDAR<br \/>\nrelease     MOV  &#xff08;&#043; \u7f16\u8bd1\u5668\u5c4f\u969c&#xff09;             STLR<br \/>\nacq_rel     MOV  &#xff08;&#043; \u7f16\u8bd1\u5668\u5c4f\u969c&#xff09;             LDAXR\/STLXR&#xff08;\u7528\u4e8e RMW&#xff09;<br \/>\nseq_cst     MOV &#043; MFENCE  \u6216  LOCK XCHG      LDAR &#043; STLR &#043; DMB&#xff08;\u53d6\u51b3\u4e8e\u4e0a\u4e0b\u6587&#xff09;<\/p>\n<p>\u8fd9\u5f20\u8868\u89e3\u91ca\u4e86\u4e00\u5207&#xff1a;\u5728 x86 \u4e0a&#xff0c;relaxed \u548c acquire\/release \u751f\u6210\u7684\u673a\u5668\u7801\u5b8c\u5168\u76f8\u540c\u3002\u00a0\u4f60\u5199\u9519\u4e86 memory order&#xff0c;\u7f16\u8bd1\u5668\u4e0d\u62a5\u9519&#xff0c;x86 \u786c\u4ef6\u4e5f\u4e0d\u62a5\u9519\u2014\u2014\u5b83\u4f1a\u7528 TSO \u7684\u5f3a\u4fdd\u8bc1\u66ff\u4f60\u515c\u5e95\u3002\u4f46\u8fd9\u4e2a\u515c\u5e95\u4e0d\u5b58\u5728\u4e8e ARM \u4e0a\u3002<\/p>\n<h4>\u90a3\u4e2a\u552f\u4e00\u7684\u4f8b\u5916&#xff1a;StoreLoad \u91cd\u6392<\/h4>\n<p>TSO \u5141\u8bb8\u7684\u552f\u4e00\u91cd\u6392\u65b9\u5411\u662f StoreLoad&#xff1a;\u6838\u5fc3 0 \u5148\u6267\u884c\u00a0store A&#xff0c;\u518d\u6267\u884c\u00a0load B&#xff0c;\u4f46\u00a0load B\u00a0\u53ef\u80fd\u5728\u00a0store A\u00a0\u5bf9\u5916\u53ef\u89c1\u4e4b\u524d\u5c31\u6267\u884c\u4e86\u3002\u8fd9\u4f1a\u5bfc\u81f4\u7ecf\u5178\u7684 Dekker\/Peterson \u7b97\u6cd5\u5728 x86 \u4e0a\u4e5f\u5931\u8d25\u2014\u2014\u662f\u7684&#xff0c;x86 \u4e5f\u4e0d\u662f\u201c\u7edd\u5bf9\u5f3a\u4e00\u81f4\u201d\u3002<\/p>\n<p>\u4f46 StoreLoad \u91cd\u6392\u5bf9\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u5f0f\u7684\u65e0\u9501\u961f\u5217\u51e0\u4e4e\u6ca1\u6709\u5f71\u54cd\u3002\u751f\u4ea7\u8005\u5148\u5199 payload&#xff0c;\u518d\u5199 tail \u6307\u9488&#xff1b;\u6d88\u8d39\u8005\u5148\u8bfb tail \u6307\u9488&#xff0c;\u518d\u8bfb payload\u3002\u8fd9\u91cc\u5173\u952e\u7684\u4f9d\u8d56\u94fe\u662f StoreStore&#xff08;\u751f\u4ea7\u8005\u7aef&#xff09;\u548c LoadLoad&#xff08;\u6d88\u8d39\u8005\u7aef&#xff09;&#xff0c;\u800c TSO \u5bf9\u8fd9\u4e24\u4e2a\u65b9\u5411\u90fd\u4fdd\u8bc1\u4e86\u987a\u5e8f\u3002\u6240\u4ee5\u65e0\u9501\u961f\u5217\u6070\u597d\u8e29\u5728\u4e86 TSO \u7684\u201c\u514d\u8d39\u4fdd\u8bc1\u201d\u533a\u95f4\u5185\u3002<\/p>\n<p>\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u4f60\u5728 x86 \u4e0a\u8dd1\u65e0\u9501\u961f\u5217\u7684\u538b\u529b\u6d4b\u8bd5&#xff0c;\u54ea\u6015 memory order \u5168\u5199\u6210\u00a0relaxed&#xff0c;\u4e5f\u5927\u6982\u7387\u8dd1\u4e0d\u51fa\u95ee\u9898\u2014\u2014\u786c\u4ef6\u5728\u66ff\u4f60\u505a\u4f60\u5fd8\u8bb0\u505a\u7684\u4e8b\u3002<\/p>\n<hr \/>\n<h3>memory_order \u4e94\u4ef6\u5957\u5728\u6536\u53d1\u7ebf\u7a0b\u6a21\u578b\u4e2d\u7684\u6b63\u786e\u6620\u5c04<\/h3>\n<p>\u628a\u4e94\u4e2a memory order \u5728\u65e0\u9501\u961f\u5217\u7684\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u578b\u4e2d\u7cbe\u786e\u6620\u5c04\u4e00\u904d\u3002\u6211\u89c1\u8fc7\u592a\u591a\u4eba\u628a\u5b83\u4eec\u5f53\u201c\u6027\u80fd\u65cb\u94ae\u201d\u2014\u2014\u201cseq_cst \u6700\u6162\u4f46\u6700\u5b89\u5168&#xff0c;relaxed \u6700\u5feb\u4f46\u6700\u5371\u9669&#xff0c;\u7528 release \u6298\u4e2d\u4e00\u4e0b\u201d\u2014\u2014\u8fd9\u662f\u9519\u8bef\u7684\u5fc3\u7406\u6a21\u578b\u3002memory order \u4e0d\u662f\u6027\u80fd\u7b49\u7ea7&#xff0c;\u662f\u540c\u6b65\u8bed\u4e49\u7684\u7cbe\u786e\u89c4\u7ea6\u3002<\/p>\n<h4>relaxed&#xff1a;\u4e0d\u5efa\u7acb\u4efb\u4f55\u8de8\u7ebf\u7a0b\u53ef\u89c1\u6027\u5173\u7cfb<\/h4>\n<p>memory_order_relaxed\u00a0\u53ea\u4fdd\u8bc1\u539f\u5b50\u6027&#xff08;\u4e0d\u4f1a\u6495\u88c2&#xff09;&#xff0c;\u4e0d\u4fdd\u8bc1\u4efb\u4f55\u6392\u5e8f\u3002\u5b83\u552f\u4e00\u5408\u6cd5\u7684\u4f7f\u7528\u573a\u666f\u662f&#xff1a;\u8fd9\u4e2a\u539f\u5b50\u53d8\u91cf\u7684\u503c**\u53ea\u5bf9\u5199\u5b83\u548c\u8bfb\u5b83\u7684\u7ebf\u7a0b\u81ea\u5df1\u6709\u610f\u4e49**&#xff0c;\u4e0d\u7528\u5b83\u6765\u201c\u4f20\u9012\u201d\u5176\u4ed6\u975e\u539f\u5b50\u6570\u636e\u7684\u53ef\u89c1\u6027\u3002<\/p>\n<p>\u5728\u65e0\u9501\u961f\u5217\u4e2d&#xff0c;\u5178\u578b\u7684\u5408\u6cd5 relaxed \u7528\u6cd5&#xff1a;<\/p>\n<p>C&#043;&#043;<\/p>\n<p>\/\/ C&#043;&#043;20, GCC 13<br \/>\n\/\/ \u751f\u4ea7\u8005\u5728 CAS \u5faa\u73af\u4e2d&#034;\u9884\u8bfb&#034;\u5f53\u524d tail&#xff0c;<br \/>\n\/\/ \u8fd9\u6b21\u8bfb\u4e0d\u9700\u8981\u770b\u5230\u6d88\u8d39\u8005\u7684\u6570\u636e\u2014\u2014\u771f\u6b63\u7684\u540c\u6b65\u70b9\u5728\u540e\u9762\u7684 CAS<br \/>\nsize_t pos &#061; tail_.load(std::memory_order_relaxed);<\/p>\n<p>\u8fd9\u91cc\u00a0tail_\u00a0\u7684 relaxed load \u662f\u5b89\u5168\u7684&#xff0c;\u56e0\u4e3a\u8fd9\u6b21\u8bfb\u4e0d\u5efa\u7acb happens-before\u2014\u2014\u540e\u9762\u90a3\u6b21 CAS&#xff08;\u5e26 release&#xff09;\u624d\u662f\u771f\u6b63\u7684\u540c\u6b65\u70b9\u3002\u5982\u679c\u8fd9\u91cc\u5199\u6210\u00a0seq_cst&#xff0c;x86 \u4e0a\u6ca1\u533a\u522b&#xff0c;ARM \u4e0a\u591a\u4e00\u6761\u00a0LDAR&#xff0c;\u767d\u767d\u6d6a\u8d39\u3002<\/p>\n<p>\u4f46\u5982\u679c\u4f60\u628a\u53d1\u5e03 tail \u7684\u90a3\u6b21 store\u00a0\u4e5f\u5199\u6210 relaxed\u2014\u2014\u5728 x86 \u4e0a\u5b8c\u5168\u6ca1\u95ee\u9898&#xff0c;ARM \u4e0a\u6d88\u8d39\u8005\u5c31\u53ef\u80fd\u770b\u5230\u65b0 tail \u4f46\u770b\u4e0d\u5230\u65b0 payload\u3002<\/p>\n<h4>acquire &#043; release&#xff1a;\u914d\u5bf9\u5efa\u7acb happens-before<\/h4>\n<p>\u8fd9\u662f\u65e0\u9501\u961f\u5217\u6700\u6838\u5fc3\u7684\u540c\u6b65\u8bed\u4e49\u3002acquire \u548c release \u5fc5\u987b\u6210\u5bf9\u51fa\u73b0&#xff0c;\u4e00\u4e2a\u5728\u5199\u7aef&#xff0c;\u4e00\u4e2a\u5728\u8bfb\u7aef&#xff0c;\u4e2d\u95f4\u901a\u8fc7\u540c\u4e00\u4e2a\u539f\u5b50\u53d8\u91cf\u4f20\u9012\u53ef\u89c1\u6027\u3002<\/p>\n<p>text<\/p>\n<p>\u751f\u4ea7\u8005\u7ebf\u7a0b                           \u6d88\u8d39\u8005\u7ebf\u7a0b<br \/>\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500                       \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\n\u5199 payload &#061; 42<br \/>\n\u2502<br \/>\n\u251c\u2500 tail.store(new_val, release) \u2500\u2500\u25b6 tail.load(acquire) \u2500\u2500\u2524<br \/>\n\u2502   happens-before \u5efa\u7acb              \u2502                    \u2502<br \/>\n\u2502                                    \u251c\u2500 \u8bfb payload<br \/>\n\u2502                                    \u2502  \u4fdd\u8bc1\u770b\u5230 42        <\/p>\n<p>release\u00a0\u7684\u8bed\u4e49\u662f&#xff1a;**\u5728\u8fd9\u6761 store \u4e4b\u524d\u7684\u6240\u6709\u5185\u5b58\u5199\u5165&#xff0c;\u5bf9\u4efb\u4f55\u540e\u7eed\u901a\u8fc7 acquire \u8bfb\u5230\u8fd9\u4e2a\u503c\u7684\u7ebf\u7a0b\u90fd\u53ef\u89c1\u3002** \u8fd9\u662f\u4e00\u4e2a\u5355\u5411\u5c4f\u969c\u2014\u2014\u5b83\u963b\u6b62 release store \u4e4b\u524d\u7684\u5199\u64cd\u4f5c\u88ab\u91cd\u6392\u5230\u5b83\u4e4b\u540e&#xff0c;\u4f46\u4e0d\u963b\u6b62\u5b83\u4e4b\u540e\u7684\u64cd\u4f5c\u88ab\u63d0\u524d\u3002<\/p>\n<p>acquire\u00a0\u7684\u8bed\u4e49\u662f\u5bf9\u79f0\u7684&#xff1a;**\u5728\u8fd9\u6761 load \u4e4b\u540e\u7684\u6240\u6709\u5185\u5b58\u8bfb\u53d6&#xff0c;\u90fd\u80fd\u770b\u5230\u5bf9\u5e94 release \u4e4b\u524d\u5199\u5165\u7684\u503c\u3002** \u5b83\u963b\u6b62 acquire load \u4e4b\u540e\u7684\u8bfb\u64cd\u4f5c\u88ab\u91cd\u6392\u5230\u5b83\u4e4b\u524d\u3002<\/p>\n<p>\u5728 ARM \u4e0a&#xff0c;\u8fd9\u4e24\u4e2a\u8bed\u4e49\u5206\u522b\u5bf9\u5e94\u00a0STLR\u00a0\u548c\u00a0LDAR\u00a0\u6307\u4ee4\u3002STLR\u00a0\u662f\u4e00\u4e2a\u201c\u653e\u884c\u680f\u6746\u201d\u2014\u2014\u5b83\u4e4b\u524d\u7684\u6240\u6709\u5199\u5165\u5fc5\u987b\u5148\u5b8c\u6210&#xff0c;\u7136\u540e\u8fd9\u6761 store \u672c\u8eab\u5b8c\u6210&#xff0c;\u4e4b\u540e\u7684\u64cd\u4f5c\u624d\u80fd\u6267\u884c\u3002LDAR\u00a0\u662f\u4e00\u4e2a\u201c\u5165\u53e3\u680f\u6746\u201d\u2014\u2014\u8fd9\u6761 load \u5b8c\u6210\u4e4b\u540e&#xff0c;\u540e\u7eed\u7684\u8bfb\u5199\u624d\u80fd\u6267\u884c\u3002<\/p>\n<h4>seq_cst&#xff1a;\u5168\u5e8f\u4e00\u81f4\u6027&#xff0c;\u4ee3\u4ef7\u6700\u9ad8<\/h4>\n<p>memory_order_seq_cst\u00a0\u5728 acquire\/release \u7684\u57fa\u7840\u4e0a\u589e\u52a0\u4e86\u4e00\u4e2a\u5168\u5c40\u5168\u5e8f&#xff1a;\u6240\u6709\u7ebf\u7a0b\u89c2\u5bdf\u5230\u7684 seq_cst \u64cd\u4f5c\u7684\u987a\u5e8f\u662f\u4e00\u81f4\u7684\u3002\u5b83\u7684\u5178\u578b\u5e94\u7528\u573a\u666f\u662f\u9700\u8981\u591a\u4e2a\u539f\u5b50\u53d8\u91cf\u4e4b\u95f4\u7ef4\u6301\u4e00\u81f4\u7684\u5168\u5c40\u89c2\u5bdf\u987a\u5e8f\u2014\u2014\u6bd4\u5982 Dekker \u4e92\u65a5\u7b97\u6cd5\u3002<\/p>\n<p>\u5728\u65e0\u9501\u961f\u5217\u4e2d&#xff0c;\u51e0\u4e4e\u4e0d\u9700\u8981 seq_cst\u3002\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u4e4b\u95f4\u53ea\u9700\u8981\u901a\u8fc7\u4e00\u4e2a\u539f\u5b50\u53d8\u91cf&#xff08;tail \u6216 head&#xff09;\u4f20\u9012\u53ef\u89c1\u6027&#xff0c;acquire\/release \u5b8c\u5168\u591f\u7528\u3002\u6211\u770b\u8fc7\u4e0d\u5c11\u4ee3\u7801\u201c\u4e3a\u4e86\u5b89\u5168\u201d\u628a\u6240\u6709\u539f\u5b50\u64cd\u4f5c\u90fd\u7528\u9ed8\u8ba4\u7684\u00a0seq_cst\u2014\u2014\u5728 x86 \u4e0a\u8fd9\u53ea\u662f\u8ba9 store \u591a\u4e86\u4e00\u6761\u00a0MFENCE&#xff08;\u6216\u8005\u7f16\u8bd1\u5668\u7528\u00a0LOCK MOV\u00a0\u4ee3\u66ff&#xff09;&#xff0c;\u5728 ARM \u4e0a\u5219\u662f\u6bcf\u4e2a store \u90fd\u53d8\u6210\u00a0STLR\u00a0&#043; \u989d\u5916\u7684\u00a0DMB ISH&#xff08;Data Memory Barrier, Inner Shareable&#xff09;&#xff0c;\u4ee3\u4ef7\u663e\u8457\u3002<\/p>\n<p>\u6211\u5728\u90a3\u4e2a\u6d88\u606f\u4e2d\u95f4\u4ef6\u7684\u79fb\u690d\u9879\u76ee\u4e2d\u505a\u8fc7\u5bf9\u6bd4\u6d4b\u8bd5&#xff1a;\u4e00\u4e2a SPSC ring buffer&#xff0c;\u628a\u6240\u6709\u00a0seq_cst\u00a0\u6362\u6210\u6b63\u786e\u7684\u00a0acquire\/release&#xff0c;\u5728 Graviton3 \u4e0a\u541e\u5410\u63d0\u5347\u4e86\u7ea6 18%\u3002x86 \u4e0a\u5dee\u5f02\u4e0d\u5230 2%&#xff0c;\u56e0\u4e3a x86 \u7684\u00a0MFENCE\u00a0\u53ea\u5728 seq_cst store \u65f6\u624d\u51fa\u73b0&#xff0c;\u800c acquire\/release \u672c\u5c31\u662f\u514d\u8d39\u7684\u3002<\/p>\n<h4>acq_rel&#xff1a;\u7528\u4e8e Read-Modify-Write \u64cd\u4f5c<\/h4>\n<p>memory_order_acq_rel\u00a0\u53ea\u7528\u5728\u00a0compare_exchange\u3001fetch_add\u00a0\u8fd9\u7c7b RMW&#xff08;Read-Modify-Write&#xff09;\u64cd\u4f5c\u4e0a\u3002\u5b83\u540c\u65f6\u5177\u5907 acquire \u548c release \u7684\u8bed\u4e49\u2014\u2014\u8fd9\u6b21\u539f\u5b50\u64cd\u4f5c\u65e2\u201c\u62ff\u5230\u201d\u4e86\u5176\u4ed6\u7ebf\u7a0b\u901a\u8fc7 release \u53d1\u5e03\u7684\u6570\u636e&#xff0c;\u4e5f\u201c\u53d1\u5e03\u201d\u4e86\u81ea\u5df1\u4e4b\u524d\u5199\u5165\u7684\u6570\u636e\u3002<\/p>\n<p>\u5728 MPMC \u961f\u5217\u4e2d&#xff0c;\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u90fd\u53ef\u80fd\u7ade\u4e89\u540c\u4e00\u4e2a tail\/head \u6307\u9488&#xff0c;CAS \u7684\u6210\u529f\u8def\u5f84\u901a\u5e38\u9700\u8981\u00a0acq_rel&#xff1a;\u6210\u529f\u7684\u90a3\u4e2a\u7ebf\u7a0b\u65e2\u8981\u770b\u5230\u524d\u4e00\u4e2a\u6210\u529f\u8005\u5199\u5165\u7684\u6570\u636e&#xff0c;\u4e5f\u8981\u628a\u81ea\u5df1\u5199\u5165\u7684\u6570\u636e\u5bf9\u540e\u7eed\u7ebf\u7a0b\u53ef\u89c1\u3002<\/p>\n<hr \/>\n<h3>ARM \u7684 LDAR\/STLR \u4e0e x86 \u7684\u5bf9\u7167\u53cd\u6c47\u7f16&#xff1a;\u7f16\u8bd1\u5668\u5230\u5e95\u63d2\u4e86\u4ec0\u4e48<\/h3>\n<p>\u4e0a\u4e00\u8282\u4ece\u8bed\u4e49\u89d2\u5ea6\u8bb2\u4e86 memory order \u7684\u6620\u5c04&#xff0c;\u8fd9\u4e00\u8282\u4ece\u673a\u5668\u7801\u5c42\u9762\u5f7b\u5e95\u770b\u6e05\u695a\u3002<\/p>\n<h4>x86-64&#xff1a;TSO \u662f\u201c\u514d\u8d39\u7684\u5348\u9910\u201d<\/h4>\n<p>\u5728 x86-64 \u4e0a&#xff0c;GCC 13 -O2 \u4e3a\u4e0d\u540c memory order \u751f\u6210\u7684\u4ee3\u7801\u5982\u4e0b&#xff08;\u4ee5\u4e00\u4e2a\u00a0std::atomic&lt;int&gt;\u00a0\u7684 store \u4e3a\u4f8b&#xff0c;\u53ef\u5728 Compiler Explorer \u590d\u9a8c&#xff09;&#xff1a;<\/p>\n<p>text<\/p>\n<p>; memory_order_relaxed store<br \/>\nmov     DWORD PTR [rdi], esi           ; \u666e\u901a MOV&#xff0c;\u65e0\u5c4f\u969c<\/p>\n<p>; memory_order_release store<br \/>\nmov     DWORD PTR [rdi], esi           ; \u8fd8\u662f\u666e\u901a MOV&#xff01;\u5b8c\u5168\u4e00\u6837<\/p>\n<p>; memory_order_seq_cst store<br \/>\nxchg    DWORD PTR [rdi], esi           ; XCHG \u81ea\u5e26 LOCK \u524d\u7f00&#xff08;\u9690\u542b\u5168\u5c4f\u969c&#xff09;<br \/>\n; \u6216\u8005:<br \/>\n; mov   DWORD PTR [rdi], esi<br \/>\n; mfence<\/p>\n<p>\u5bf9\u4e8e load&#xff1a;<\/p>\n<p>text<\/p>\n<p>; memory_order_relaxed load<br \/>\nmov     eax, DWORD PTR [rdi]           ; \u666e\u901a MOV<\/p>\n<p>; memory_order_acquire load<br \/>\nmov     eax, DWORD PTR [rdi]           ; \u8fd8\u662f\u666e\u901a MOV<\/p>\n<p>; memory_order_seq_cst load<br \/>\nmov     eax, DWORD PTR [rdi]           ; \u8fd8\u662f\u666e\u901a MOV&#xff08;TSO \u4fdd\u8bc1\u4e86 load \u987a\u5e8f&#xff09;<\/p>\n<p>\u4f60\u770b\u5230\u4e86\u5417&#xff1f;\u5728 x86 \u4e0a&#xff0c;relaxed\u3001acquire\u3001release \u4e09\u4e2a\u7ea7\u522b\u7684 load\/store \u751f\u6210\u7684\u673a\u5668\u7801\u5b8c\u5168\u4e00\u6837\u3002\u00a0\u8fd9\u610f\u5473\u7740\u4f60\u5728 x86 \u4e0a\u5b8c\u5168\u65e0\u6cd5\u901a\u8fc7\u8fd0\u884c\u65f6\u884c\u4e3a\u6765\u533a\u5206\u5b83\u4eec\u2014\u2014\u65e0\u8bba\u4f60\u5199\u9519\u4e86\u54ea\u4e2a&#xff0c;\u8dd1\u51fa\u6765\u7684\u884c\u4e3a\u90fd\u662f\u201c\u6b63\u786e\u201d\u7684&#xff0c;\u56e0\u4e3a\u786c\u4ef6\u5728\u515c\u5e95\u3002<\/p>\n<h4>AArch64&#xff1a;\u6bcf\u4e2a\u7ea7\u522b\u90fd\u5bf9\u5e94\u4e0d\u540c\u7684\u6307\u4ee4<\/h4>\n<p>\u540c\u6837\u7684\u64cd\u4f5c\u5728 AArch64 \u4e0a&#xff1a;<\/p>\n<p>text<\/p>\n<p>; memory_order_relaxed store<br \/>\nstr     w1, [x0]                       ; \u666e\u901a STR<\/p>\n<p>; memory_order_release store<br \/>\nstlr    w1, [x0]                       ; STLR &#061; Store-Release<br \/>\n                                       ; \u4fdd\u8bc1\u6b64\u524d\u7684\u6240\u6709\u5185\u5b58\u5199\u5165\u5bf9\u5916\u53ef\u89c1\u540e\u624d\u6267\u884c\u6b64 store<\/p>\n<p>; memory_order_seq_cst store<br \/>\nstlr    w1, [x0]                       ; \u4e5f\u662f STLR&#xff08;\u4f46\u914d\u5408 load \u7aef\u7684 LDAR \u5efa\u7acb\u5168\u5e8f&#xff09;<\/p>\n<p>text<\/p>\n<p>; memory_order_relaxed load<br \/>\nldr     w0, [x0]                       ; \u666e\u901a LDR<\/p>\n<p>; memory_order_acquire load<br \/>\nldar    w0, [x0]                       ; LDAR &#061; Load-Acquire<br \/>\n                                       ; \u4fdd\u8bc1\u6b64 load \u5b8c\u6210\u540e\u624d\u6267\u884c\u540e\u7eed\u7684\u5185\u5b58\u8bbf\u95ee<\/p>\n<p>; memory_order_seq_cst load<br \/>\nldar    w0, [x0]                       ; \u4e5f\u662f LDAR<\/p>\n<p>STR\u00a0\u548c\u00a0STLR\u00a0\u662f\u4e24\u6761**\u5b8c\u5168\u4e0d\u540c\u7684\u6307\u4ee4**&#xff0c;CPU \u6d41\u6c34\u7ebf\u5bf9\u5b83\u4eec\u7684\u5904\u7406\u65b9\u5f0f\u4e0d\u540c\u3002STR\u00a0\u662f\u4e00\u6761\u666e\u901a\u7684\u5199\u5165&#xff0c;CPU \u53ef\u4ee5\u81ea\u7531\u5730\u628a\u5b83\u91cd\u6392\u5230\u540e\u7eed\u7684\u8bfb\u5199\u4e4b\u540e&#xff08;\u53ea\u8981\u5bf9\u5355\u7ebf\u7a0b\u7684\u53ef\u89c1\u6027\u6ca1\u6709\u5f71\u54cd&#xff09;\u3002STLR\u00a0\u5219\u5e26\u6709\u5355\u5411\u5c4f\u969c\u8bed\u4e49\u2014\u2014CPU \u5fc5\u987b\u4fdd\u8bc1\u5728\u6267\u884c\u00a0STLR\u00a0\u4e4b\u524d&#xff0c;\u6240\u6709\u5148\u524d\u7684\u5185\u5b58\u64cd\u4f5c\u90fd\u5df2\u7ecf\u5b8c\u6210\u3002<\/p>\n<p>LDAR\u00a0\u540c\u7406&#xff1a;\u666e\u901a\u7684\u00a0LDR\u00a0\u53ef\u4ee5\u88ab CPU \u91cd\u6392\u5230\u524d\u9762\u7684\u5199\u64cd\u4f5c\u4e4b\u524d&#xff08;ARM \u5141\u8bb8 LoadLoad \u548c LoadStore \u91cd\u6392&#xff09;&#xff0c;\u800c\u00a0LDAR\u00a0\u5efa\u7acb\u4e86\u4e00\u4e2a\u201c\u4e0d\u53ef\u7a7f\u8d8a\u201d\u7684\u8fb9\u754c\u3002<\/p>\n<h4>ARMv8.1 \u7684\u8fdb\u5316&#xff1a;LDAPR&#xff08;Acquire \u7684\u5f31\u5316\u7248&#xff09;<\/h4>\n<p>ARMv8.1 \u5f15\u5165\u4e86\u00a0LDAPR&#xff08;Load-AcquirePc&#xff09;&#xff0c;\u6bd4\u00a0LDAR\u00a0\u5f31&#xff1a;\u5b83\u53ea\u963b\u6b62 LoadLoad \u548c LoadStore \u91cd\u6392&#xff0c;\u4e0d\u963b\u6b62 StoreStore \u91cd\u6392\u3002GCC \u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u4f1a\u628a\u00a0memory_order_acquire\u00a0\u7684 load \u7f16\u8bd1\u6210\u00a0LDAPR\u00a0\u800c\u4e0d\u662f\u00a0LDAR&#xff0c;\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\u3002\u4f46\u00a0LDAR\u00a0\u4ecd\u7136\u662f\u6700\u5b89\u5168\u7684\u9009\u62e9\u3002<\/p>\n<h4>\u5b9e\u9645\u5f71\u54cd&#xff1a;\u4e00\u6b21 CAS \u5728\u4e24\u4e2a\u67b6\u6784\u4e0a\u7684\u5dee\u5f02<\/h4>\n<p>\u770b\u4e00\u4e2a\u00a0compare_exchange_weak\u00a0\u5728\u4e24\u4e2a\u67b6\u6784\u4e0a\u7684\u7f16\u8bd1\u7ed3\u679c&#xff1a;<\/p>\n<p>C&#043;&#043;<\/p>\n<p>\/\/ C&#043;&#043;20, GCC 13, -O2<br \/>\nbool try_enqueue(std::atomic&lt;size_t&gt;&amp; tail, size_t expected, size_t desired) {<br \/>\n    return tail.compare_exchange_weak(<br \/>\n        expected, desired,<br \/>\n        std::memory_order_release,   \/\/ \u6210\u529f<br \/>\n        std::memory_order_relaxed    \/\/ \u5931\u8d25<br \/>\n    );<br \/>\n}<\/p>\n<p>x86-64&#xff1a;<\/p>\n<p>text<\/p>\n<p>try_enqueue:<br \/>\n    mov     rax, rsi                   ; expected \u2192 rax<br \/>\n    lock cmpxchg [rdi], rdx            ; LOCK CMPXCHG&#xff08;\u81ea\u5e26\u5168\u5c4f\u969c&#xff0c;\u6bd4 release \u66f4\u5f3a&#xff09;<br \/>\n    sete    al<br \/>\n    ret<\/p>\n<p>x86 \u7684\u00a0LOCK CMPXCHG\u00a0\u672c\u8eab\u5c31\u662f\u4e00\u4e2a\u5168\u5c4f\u969c\u64cd\u4f5c\u2014\u2014\u5b83\u65e2\u662f acquire \u4e5f\u662f release&#xff0c;\u751a\u81f3\u6bd4\u4f60\u8bf7\u6c42\u7684 release \u66f4\u5f3a\u3002**\u5728 x86 \u4e0a&#xff0c;CAS \u7684 memory order \u53c2\u6570\u51e0\u4e4e\u4e0d\u5f71\u54cd\u751f\u6210\u7684\u4ee3\u7801\u3002**<\/p>\n<p>AArch64&#xff1a;<\/p>\n<p>text<\/p>\n<p>try_enqueue:<br \/>\n.L1:<br \/>\n    ldxr    x3, [x0]                   ; LL: Load-Exclusive&#xff08;relaxed&#xff09;<br \/>\n    cmp     x3, x1<br \/>\n    b.ne    .L2<br \/>\n    stlxr   w4, x2, [x0]              ; SC: Store-Exclusive-Release<br \/>\n    cbnz    w4, .L1                    ; SC \u5931\u8d25\u91cd\u8bd5&#xff08;\u4f2a\u5931\u8d25&#xff09;<br \/>\n    mov     w0, #1<br \/>\n    ret<br \/>\n.L2:<br \/>\n    clrex                              ; \u6e05\u9664 exclusive monitor<br \/>\n    mov     w0, #0<br \/>\n    ret<\/p>\n<p>ARM \u7528 LL\/SC&#xff08;Load-Linked\/Store-Conditional&#xff09;\u6765\u5b9e\u73b0 CAS\u3002\u6ce8\u610f\u00a0LDXR\u00a0\u662f\u4e0d\u5e26 acquire \u7684&#xff08;\u56e0\u4e3a\u5931\u8d25\u8def\u5f84\u662f relaxed&#xff09;&#xff0c;\u800c\u00a0STLXR\u00a0\u5e26\u4e86 release \u540e\u7f00\u3002\u5982\u679c\u4f60\u628a\u6210\u529f\u8def\u5f84\u4e5f\u5199\u6210 relaxed&#xff0c;\u8fd9\u91cc\u5c31\u4f1a\u53d8\u6210\u666e\u901a\u7684\u00a0STXR\u2014\u2014\u5c11\u4e86 release \u8bed\u4e49&#xff0c;\u6d88\u8d39\u8005\u5c31\u53ef\u80fd\u770b\u5230\u8fc7\u671f\u7684 payload\u3002<\/p>\n<hr \/>\n<h3>\u4e00\u4e2a\u201c\u6b63\u786e\u7684\u201d\u65e0\u9501\u961f\u5217\u4e3a\u4ec0\u4e48\u5728 ARM \u4e0a\u70b8&#xff1a;\u5177\u4f53\u7684\u5931\u6548\u8def\u5f84<\/h3>\n<p>\u8bb2\u5b8c\u4e86\u539f\u7406&#xff0c;\u4e0b\u9762\u5177\u4f53\u63a8\u6f14\u4e00\u4e2a\u6709 bug \u7684\u65e0\u9501\u961f\u5217\u5728 ARM \u4e0a\u7684\u5931\u6548\u8def\u5f84\u3002\u4e0d\u662f\u5047\u8bbe&#xff0c;\u662f\u771f\u5b9e\u4f1a\u53d1\u751f\u7684\u65f6\u5e8f\u3002<\/p>\n<p>\u5047\u8bbe\u4e00\u4e2a SPSC \u73af\u5f62\u961f\u5217&#xff0c;\u5f00\u53d1\u8005\u7528\u4e86 relaxed store \u6765\u53d1\u5e03 tail&#xff1a;<\/p>\n<p>C&#043;&#043;<\/p>\n<p>\/\/ BUG: release \u88ab\u8bef\u5199\u4e3a relaxed<br \/>\n\/\/ C&#043;&#043;20, GCC 13<br \/>\ntemplate&lt;typename T, size_t N&gt;<br \/>\nclass BrokenSPSC {<br \/>\n    alignas(64) std::atomic&lt;size_t&gt; head_{0};<br \/>\n    alignas(64) std::atomic&lt;size_t&gt; tail_{0};<br \/>\n    T buffer_[N];<\/p>\n<p>public:<br \/>\n    bool push(const T&amp; val) {<br \/>\n        size_t t &#061; tail_.load(std::memory_order_relaxed);<br \/>\n        if (t &#8211; head_.load(std::memory_order_acquire) &gt;&#061; N)<br \/>\n            return false;  \/\/ \u6ee1<br \/>\n        buffer_[t % N] &#061; val;<br \/>\n        \/\/ BUG: \u8fd9\u91cc\u5e94\u8be5\u662f release&#xff0c;\u4e0d\u662f relaxed<br \/>\n        tail_.store(t &#043; 1, std::memory_order_relaxed);<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    bool pop(T&amp; val) {<br \/>\n        size_t h &#061; head_.load(std::memory_order_relaxed);<br \/>\n        if (h &#061;&#061; tail_.load(std::memory_order_acquire))<br \/>\n            return false;  \/\/ \u7a7a<br \/>\n        val &#061; buffer_[h % N];<br \/>\n        head_.store(h &#043; 1, std::memory_order_release);<br \/>\n        return true;<br \/>\n    }<br \/>\n};<\/p>\n<p>\u5728 x86 \u4e0a&#xff0c;tail_.store(t &#043; 1, relaxed)\u00a0\u7f16\u8bd1\u6210\u00a0MOV\u3002\u7531\u4e8e TSO \u4fdd\u8bc1 StoreStore \u4e0d\u91cd\u6392&#xff0c;buffer_[t % N] &#061; val\u00a0\u7684\u5199\u5165\u4e00\u5b9a\u5728\u00a0tail_.store\u00a0\u4e4b\u524d\u5bf9\u5916\u53ef\u89c1\u3002\u6d88\u8d39\u8005\u901a\u8fc7\u00a0tail_.load(acquire)\u00a0\u8bfb\u5230\u65b0 tail \u65f6&#xff0c;payload \u5df2\u7ecf\u5728\u90a3\u91cc\u4e86\u3002**\u6d4b\u4e0d\u51fa bug\u3002**<\/p>\n<p>\u5728 ARM \u4e0a&#xff0c;tail_.store(t &#043; 1, relaxed)\u00a0\u7f16\u8bd1\u6210\u00a0STR\u3002ARM \u5141\u8bb8 StoreStore \u91cd\u6392\u2014\u2014CPU \u53ef\u4ee5\u5728\u00a0buffer_[t % N] &#061; val\u00a0\u7684\u5199\u5165\u5230\u8fbe\u7f13\u5b58\u4e4b\u524d&#xff0c;\u5c31\u5148\u628a\u00a0tail_\u00a0\u7684\u65b0\u503c\u5199\u51fa\u53bb\u3002\u6d88\u8d39\u8005\u5728\u53e6\u4e00\u4e2a\u6838\u4e0a\u901a\u8fc7\u00a0LDAR\u00a0\u8bfb\u5230\u4e86\u65b0 tail&#xff0c;\u7acb\u523b\u53bb\u8bfb\u00a0buffer_[h % N]&#xff0c;\u8bfb\u5230\u7684\u662f**\u4e0a\u4e00\u8f6e\u7684\u65e7\u503c**\u6216\u8005\u672a\u521d\u59cb\u5316\u7684\u5783\u573e\u3002<\/p>\n<p>text<\/p>\n<p>\u65f6\u5e8f&#xff08;ARM \u4e0a\u53ef\u80fd\u53d1\u751f&#xff09;&#xff1a;<\/p>\n<p>\u751f\u4ea7\u8005\u6838 (Core 0)                    \u6d88\u8d39\u8005\u6838 (Core 1)<br \/>\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500                    \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\nbuffer_[0] &#061; val    \u2500\u2500\u2510<br \/>\n                      \u2502 CPU \u91cd\u6392&#xff01;<br \/>\ntail_.store(1, STR) \u2500\u2500\u2518\u2500\u2500\u25b6 \u5148\u5230\u8fbe     tail_.load(LDAR) \u2192 1<br \/>\n                          \u5185\u5b58\/\u7f13\u5b58   val &#061; buffer_[0]  \u2192 ??  \u2190 \u5783\u573e&#xff01;<br \/>\n                                      &#xff08;payload \u7684\u5199\u5165\u8fd8\u6ca1\u4f20\u64ad\u5230 Core 1&#xff09;<\/p>\n<p>\u8fd9\u4e2a bug \u6709\u51e0\u4e2a\u7279\u5f81\u4f7f\u5b83\u6781\u96be\u6392\u67e5&#xff1a;<\/p>\n<li>\n<p>\u95f4\u6b47\u6027&#xff1a;\u53ea\u6709\u5728 CPU \u6070\u597d\u9009\u62e9\u91cd\u6392 StoreStore \u65f6\u624d\u4f1a\u89e6\u53d1&#xff0c;\u6982\u7387\u8ddf\u8d1f\u8f7d\u3001\u6838\u5fc3\u6570\u3001\u7f13\u5b58\u538b\u529b\u6709\u5173\u3002\u6211\u89c2\u5bdf\u5230\u7684\u9891\u7387\u5927\u7ea6\u662f\u6bcf\u767e\u4e07\u6b21\u64cd\u4f5c\u51fa\u4e00\u6b21\u3002<\/p>\n<\/li>\n<li>\n<p>\u52a0 printf \u5c31\u6d88\u5931&#xff1a;printf\u00a0\u6d89\u53ca I\/O \u7cfb\u7edf\u8c03\u7528&#xff0c;\u5185\u6838\u4f1a\u6267\u884c\u5185\u5b58\u5c4f\u969c&#xff0c;\u628a\u4f60\u6f0f\u6389\u7684\u540c\u6b65\u201c\u8865\u4e0a\u201d\u4e86\u3002<\/p>\n<\/li>\n<li>\n<p>\u964d\u4f18\u5316\u7ea7\u522b\u5c31\u6d88\u5931&#xff1a;-O0\u00a0\u65f6\u7f16\u8bd1\u5668\u4e0d\u505a\u6307\u4ee4\u91cd\u6392&#xff0c;\u52a0\u4e0a\u9891\u7e41\u7684\u6808\u6ea2\u51fa&#xff0c;\u5404\u79cd\u526f\u4f5c\u7528\u4f1a\u201c\u78b0\u5de7\u201d\u4fdd\u8bc1\u987a\u5e8f\u3002<\/p>\n<\/li>\n<li>\n<p>TSan \u53ef\u80fd\u6293\u4e0d\u5230&#xff1a;ThreadSanitizer \u80fd\u68c0\u6d4b data race&#xff0c;\u4f46\u5982\u679c\u4f60\u7528\u4e86 atomic&#xff08;\u54ea\u6015\u662f relaxed&#xff09;&#xff0c;TSan \u8ba4\u4e3a\u4f60\u201c\u6709\u610f\u8bc6\u5730\u9009\u62e9\u4e86 relaxed \u8bed\u4e49\u201d&#xff0c;\u4e0d\u4e00\u5b9a\u62a5\u9519\u3002<\/p>\n<\/li>\n<p>\u4fee\u590d\u53ea\u9700\u8981\u4e00\u4e2a\u8bcd&#xff1a;\u628a\u00a0tail_.store(t &#043; 1, std::memory_order_relaxed)\u00a0\u6539\u6210\u00a0tail_.store(t &#043; 1, std::memory_order_release)\u3002ARM \u4e0a\u7684\u00a0STR\u00a0\u53d8\u6210\u00a0STLR&#xff0c;StoreStore \u91cd\u6392\u88ab\u963b\u6b62\u3002<\/p>\n<hr \/>\n<h3>MPMC \u7684\u771f\u5b9e\u4ee3\u4ef7&#xff1a;\u6bcf\u591a\u4e00\u4e2a\u7ade\u4e89\u8005&#xff0c;CAS \u91cd\u8bd5\u7ffb\u4e00\u500d<\/h3>\n<p>\u5728\u8ba8\u8bba\u201c\u8be5\u7528 SPSC \u8fd8\u662f MPMC\u201d\u4e4b\u524d&#xff0c;\u5148\u641e\u6e05\u695a MPMC \u961f\u5217\u5230\u5e95\u5728\u4e3a\u4ec0\u4e48\u4ed8\u51fa\u4ee3\u4ef7\u3002<\/p>\n<p>\u4e00\u4e2a\u5178\u578b\u7684 bounded MPMC \u961f\u5217&#xff08;Vyukov \u98ce\u683c&#xff0c;\u7b80\u5316\u81ea Dmitry Vyukov \u5728 1024cores.net \u4e0a\u7684\u7ecf\u5178\u8bbe\u8ba1&#xff09;\u7684\u5165\u961f\u64cd\u4f5c&#xff1a;<\/p>\n<p>C&#043;&#043;<\/p>\n<p>\/\/ C&#043;&#043;20, GCC 13\u3002\u7b80\u5316\u81ea Vyukov bounded MPMC queue<br \/>\n\/\/ \u4ec5\u5c55\u793a\u6838\u5fc3\u5165\u961f\u903b\u8f91&#xff0c;\u7701\u7565\u7c7b\u5b9a\u4e49\u548c\u51fa\u961f<br \/>\nstruct Cell {<br \/>\n    std::atomic&lt;size_t&gt; sequence;<br \/>\n    T data;<br \/>\n};<\/p>\n<p>bool enqueue(const T&amp; val) {<br \/>\n    Cell* cells &#061; cells_;<br \/>\n    size_t mask &#061; mask_;<br \/>\n    size_t pos &#061; enqueue_pos_.load(std::memory_order_relaxed);<\/p>\n<p>    for (;;) {<br \/>\n        Cell&amp; cell &#061; cells[pos &amp; mask];<br \/>\n        size_t seq &#061; cell.sequence.load(std::memory_order_acquire);<br \/>\n        intptr_t diff &#061; static_cast&lt;intptr_t&gt;(seq) &#8211; static_cast&lt;intptr_t&gt;(pos);<\/p>\n<p>        if (diff &#061;&#061; 0) {<br \/>\n            \/\/ \u8fd9\u4e2a slot \u53ef\u7528&#xff0c;\u5c1d\u8bd5 CAS \u5360\u4f4d<br \/>\n            if (enqueue_pos_.compare_exchange_weak(<br \/>\n                    pos, pos &#043; 1, std::memory_order_relaxed)) {<br \/>\n                \/\/ \u5360\u4f4d\u6210\u529f&#xff0c;\u5199\u5165\u6570\u636e<br \/>\n                cell.data &#061; val;<br \/>\n                \/\/ \u53d1\u5e03&#xff1a;\u8ba9\u6d88\u8d39\u8005\u770b\u5230<br \/>\n                cell.sequence.store(pos &#043; 1, std::memory_order_release);<br \/>\n                return true;<br \/>\n            }<br \/>\n            \/\/ CAS \u5931\u8d25&#xff1a;\u522b\u7684\u751f\u4ea7\u8005\u62a2\u5148\u4e86&#xff0c;pos \u5df2\u88ab\u5237\u65b0&#xff0c;\u91cd\u8bd5<br \/>\n        } else if (diff &lt; 0) {<br \/>\n            \/\/ \u961f\u5217\u6ee1<br \/>\n            return false;<br \/>\n        } else {<br \/>\n            \/\/ diff &gt; 0&#xff1a;\u53e6\u4e00\u4e2a\u751f\u4ea7\u8005\u521a\u5728\u5199\u3001\u8fd8\u6ca1\u53d1\u5e03&#xff0c;\u91cd\u8bfb pos<br \/>\n            pos &#061; enqueue_pos_.load(std::memory_order_relaxed);<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c55\u793a\u4e86 MPMC \u7684\u6838\u5fc3\u5f00\u9500&#xff1a;**\u6bcf\u4e2a\u751f\u4ea7\u8005\u90fd\u8981\u901a\u8fc7 CAS \u7ade\u4e89\u00a0enqueue_pos_**\u3002\u5f53\u6709 N \u4e2a\u751f\u4ea7\u8005\u540c\u65f6\u5165\u961f\u65f6&#xff1a;<\/p>\n<li>\n<p>CAS \u7ade\u4e89\u662f\u4e32\u884c\u5316\u70b9\u3002\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u751f\u4ea7\u8005\u80fd CAS \u6210\u529f&#xff0c;\u5176\u4f59 N-1 \u4e2a\u90fd\u5931\u8d25\u91cd\u8bd5\u3002\u9ad8\u7ade\u4e89\u4e0b\u5e73\u5747\u91cd\u8bd5\u6b21\u6570\u6b63\u6bd4\u4e8e N\u3002<\/p>\n<\/li>\n<li>\n<p>**enqueue_pos_\u00a0\u6240\u5728\u7684 cache line \u5728\u6838\u95f4\u5f39\u8df3**\u3002\u6bcf\u6b21 CAS&#xff08;\u65e0\u8bba\u6210\u529f\u5931\u8d25&#xff09;\u90fd\u4f1a\u628a\u8fd9\u6761 cache line \u4ece\u4e00\u4e2a\u6838\u7684 L1 \u62c9\u5230\u53e6\u4e00\u4e2a\u6838\u7684 L1&#xff0c;\u89e6\u53d1 cache coherence \u534f\u8bae&#xff08;MESI \u7684 Invalidate\/Share \u5f80\u8fd4&#xff09;\u3002\u5728\u5178\u578b\u7684 Intel Xeon \u4e0a\u4e00\u6b21\u8de8 NUMA \u7684 cache line \u4f20\u8f93\u7ea6 100-200 \u4e2a\u5468\u671f&#xff0c;\u8fd9\u76f4\u63a5\u53e0\u52a0\u5230\u5ef6\u8fdf\u4e0a\u3002<\/p>\n<\/li>\n<li>\n<p>**\u6bcf\u4e2a Cell \u7684\u00a0sequence\u00a0\u4e5f\u662f\u7ade\u4e89\u70b9**\u3002\u6d88\u8d39\u7aef\u540c\u6837\u6709\u7c7b\u4f3c\u7684 CAS \u7ade\u4e89\u00a0dequeue_pos_\u3002<\/p>\n<\/li>\n<p>\u6211\u505a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684 benchmark&#xff08;GCC 13, -O2, Intel Xeon Gold 6348, 2 NUMA nodes, 56 cores&#xff09;&#xff1a;\u56fa\u5b9a\u961f\u5217\u5bb9\u91cf 65536&#xff0c;\u6bcf\u4e2a\u7ebf\u7a0b\u5165\u961f 10M \u6761\u6d88\u606f&#xff0c;\u6d4b\u603b\u541e\u5410\u3002<\/p>\n<p>text<\/p>\n<p>\u7ebf\u7a0b\u6570 (\u751f\u4ea7\u8005)    MPMC ops\/sec      SPSC ops\/sec (\u5bf9\u7167)<br \/>\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500    \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500     \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\n1                ~180M              ~320M<br \/>\n2                ~120M              N\/A (SPSC \u5355\u751f\u4ea7\u8005)<br \/>\n4                ~65M               N\/A<br \/>\n8                ~28M               N\/A<br \/>\n16               ~11M               N\/A<\/p>\n<p>MPMC \u5728 8 \u4e2a\u751f\u4ea7\u8005\u65f6\u541e\u5410\u5df2\u7ecf\u4e0d\u5230\u5355\u7ebf\u7a0b\u7684 1\/6\u3002CAS \u91cd\u8bd5\u548c cache line bouncing \u662f\u5e73\u65b9\u7ea7\u7684\u4ee3\u4ef7&#xff0c;\u4e0d\u662f\u7ebf\u6027\u7684\u3002<\/p>\n<p>\u800c SPSC \u7684\u5355\u7ebf\u7a0b\u541e\u5410\u63a5\u8fd1 320M ops\/sec\u2014\u2014\u56e0\u4e3a\u5b83\u6839\u672c\u6ca1\u6709 CAS\u3002\u751f\u4ea7\u8005\u65e0\u7ade\u4e89\u5730\u5199 tail&#xff0c;\u6d88\u8d39\u8005\u65e0\u7ade\u4e89\u5730\u5199 head&#xff0c;\u4e24\u4e2a\u6307\u9488\u5728\u4e0d\u540c\u7684 cache line \u4e0a&#xff0c;\u51e0\u4e4e\u6ca1\u6709\u8de8\u6838\u901a\u4fe1\u3002<\/p>\n<hr \/>\n<h3>\u4e3a\u4ec0\u4e48\u201c\u4e00\u4e2a IO \u7ebf\u7a0b\u5582\u591a\u4e2a worker\u201d\u5e94\u8be5\u7528 MPSC \u800c\u975e MPMC<\/h3>\n<p>\u7f51\u7edc\u7f16\u7a0b\u4e2d\u6700\u5e38\u89c1\u7684\u7ebf\u7a0b\u6a21\u578b\u662f&#xff1a;\u4e00\u4e2a&#xff08;\u6216\u5c11\u91cf&#xff09;IO \u7ebf\u7a0b\u8d1f\u8d23\u6536\u5305\/\u89e3\u5305&#xff0c;\u7136\u540e\u628a\u6d88\u606f\u5206\u53d1\u7ed9 N \u4e2a worker \u7ebf\u7a0b\u5904\u7406\u3002\u5f88\u591a\u4eba\u7b2c\u4e00\u53cd\u5e94\u662f\u7528\u4e00\u4e2a MPMC \u961f\u5217&#xff1a;IO \u7ebf\u7a0b\u5f80\u91cc\u6254&#xff0c;N \u4e2a worker \u7ade\u4e89\u7740\u53d6\u3002<\/p>\n<p>\u4f46\u8fd9\u4e2a\u6a21\u578b\u7684\u6027\u80fd\u74f6\u9888\u5728\u6d88\u8d39\u7aef\u7684 CAS \u7ade\u4e89\u2014\u2014N \u4e2a worker \u90fd\u5728\u62a2\u00a0dequeue_pos_\u3002\u8fd9\u8ddf\u4e0a\u4e00\u8282\u5206\u6790\u7684\u751f\u4ea7\u7aef CAS \u7ade\u4e89\u5b8c\u5168\u5bf9\u79f0&#xff0c;\u5f00\u9500\u968f N \u589e\u957f\u3002<\/p>\n<p>\u66f4\u597d\u7684\u67b6\u6784\u662f&#xff1a;\u6bcf\u4e2a worker \u4e00\u4e2a MPSC \u961f\u5217\u3002IO \u7ebf\u7a0b\u6839\u636e\u8fde\u63a5\u4eb2\u548c\u6027\u628a\u6d88\u606f\u63a8\u5230\u5bf9\u5e94 worker \u7684\u961f\u5217\u91cc\u3002<\/p>\n<p>text<\/p>\n<p>\u65b9\u6848 A&#xff1a;\u4e00\u4e2a MPMC \u961f\u5217&#xff08;\u7ade\u4e89\u4e25\u91cd&#xff09;<\/p>\n<p>                 \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n  IO Thread \u2500\u2500\u25b6  \u2502 MPMC Queue\u2502 \u2500\u2500\u25b6 Worker 0<br \/>\n                 \u2502           \u2502 \u2500\u2500\u25b6 Worker 1<br \/>\n                 \u2502           \u2502 \u2500\u2500\u25b6 Worker 2<br \/>\n                 \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518     &#8230; N workers \u7ade\u4e89 dequeue<\/p>\n<p>\u65b9\u6848 B&#xff1a;N \u4e2a MPSC \u961f\u5217&#xff08;\u65e0\u6d88\u8d39\u7aef\u7ade\u4e89&#xff09;<\/p>\n<p>  IO Thread \u2500\u2500\u25b6 MPSC Queue 0 \u2500\u2500\u25b6 Worker 0 &#xff08;\u72ec\u5360\u6d88\u8d39&#xff09;<br \/>\n            \u2500\u2500\u25b6 MPSC Queue 1 \u2500\u2500\u25b6 Worker 1 &#xff08;\u72ec\u5360\u6d88\u8d39&#xff09;<br \/>\n            \u2500\u2500\u25b6 MPSC Queue 2 \u2500\u2500\u25b6 Worker 2 &#xff08;\u72ec\u5360\u6d88\u8d39&#xff09;<\/p>\n<p>\u65b9\u6848 B \u7684\u4f18\u52bf&#xff1a;<\/p>\n<li>\n<p>\u6d88\u8d39\u7aef\u96f6\u7ade\u4e89\u3002\u6bcf\u4e2a worker \u72ec\u5360\u6d88\u8d39\u81ea\u5df1\u7684\u961f\u5217&#xff0c;head_\u00a0\u6c38\u8fdc\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u5199&#xff0c;\u4e0d\u9700\u8981 CAS\u2014\u2014\u76f4\u63a5\u00a0head_.store(h &#043; 1, release)\u00a0\u5373\u53ef\u3002\u8fd9\u4f7f\u5f97\u6d88\u8d39\u7aef\u7684\u5f00\u9500\u964d\u5230\u8ddf SPSC \u4e00\u6837\u3002<\/p>\n<\/li>\n<li>\n<p>\u751f\u4ea7\u7aef\u7ade\u4e89\u964d\u4f4e\u3002\u5982\u679c\u6709\u591a\u4e2a IO \u7ebf\u7a0b&#xff08;\u6bd4\u5982 SO_REUSEPORT \u6a21\u5f0f&#xff09;&#xff0c;\u6bcf\u4e2a IO \u7ebf\u7a0b\u63a8\u5230\u4e0d\u540c worker \u961f\u5217\u7684\u6982\u7387\u5206\u6563\u4e86\u7ade\u4e89\u3002\u5373\u4f7f\u6240\u6709 IO \u7ebf\u7a0b\u90fd\u53ef\u80fd\u63a8\u540c\u4e00\u4e2a worker&#xff0c;MPSC \u7684\u751f\u4ea7\u7aef CAS \u7ade\u4e89\u4e5f\u53ea\u5728 IO \u7ebf\u7a0b\u4e4b\u95f4&#xff0c;\u4e0d\u50cf MPMC \u7684\u6d88\u8d39\u7aef\u90a3\u6837\u7275\u6d89\u6240\u6709 worker\u3002<\/p>\n<\/li>\n<li>\n<p>\u8fde\u63a5\u4eb2\u548c\u6027\u3002\u540c\u4e00\u4e2a TCP \u8fde\u63a5\u7684\u6240\u6709\u6d88\u606f\u53ef\u4ee5\u8def\u7531\u5230\u540c\u4e00\u4e2a worker&#xff0c;\u4fdd\u8bc1\u6709\u5e8f\u5904\u7406&#xff0c;\u4e0d\u9700\u8981\u989d\u5916\u7684\u5e8f\u5217\u53f7\u6392\u5e8f\u3002\u8fd9\u5728\u534f\u8bae\u89e3\u6790\u3001\u4f1a\u8bdd\u72b6\u6001\u7ba1\u7406\u573a\u666f\u4e0b\u6781\u5176\u91cd\u8981\u2014\u2014MPMC \u6a21\u5f0f\u4e0b\u540c\u4e00\u4e2a\u8fde\u63a5\u7684\u6d88\u606f\u53ef\u80fd\u88ab\u4e0d\u540c worker \u62a2\u5230&#xff0c;\u5bfc\u81f4\u4e71\u5e8f\u3002<\/p>\n<\/li>\n<li>\n<p>cache \u5c40\u90e8\u6027\u3002\u6bcf\u4e2a worker \u53ea\u8bfb\u81ea\u5df1\u7684\u961f\u5217&#xff0c;head \u6307\u9488\u548c\u961f\u5217 buffer \u957f\u671f\u9a7b\u7559\u5728\u81ea\u5df1\u6838\u5fc3\u7684 L1\/L2 cache \u4e2d&#xff0c;\u51e0\u4e4e\u4e0d\u89e6\u53d1\u8de8\u6838\u4f20\u8f93\u3002<\/p>\n<\/li>\n<h4>MPSC \u961f\u5217\u7684 consumer \u7aef\u6709\u591a\u7b80\u5355<\/h4>\n<p>MPSC \u7684\u6d88\u8d39\u7aef\u9000\u5316\u6210\u4e86 SPSC \u7684\u6d88\u8d39\u7aef\u2014\u2014\u56e0\u4e3a\u53ea\u6709\u4e00\u4e2a\u6d88\u8d39\u8005\u3002\u8fd9\u610f\u5473\u7740\u6d88\u8d39\u7aef\u7684\u4ee3\u7801\u53ef\u4ee5\u6781\u5ea6\u7b80\u5316&#xff1a;<\/p>\n<p>C&#043;&#043;<\/p>\n<p>\/\/ C&#043;&#043;20, GCC 13<br \/>\n\/\/ MPSC \u961f\u5217\u7684\u6d88\u8d39\u7aef\u2014\u2014\u53ea\u6709\u4e00\u4e2a\u6d88\u8d39\u8005&#xff0c;\u4e0d\u9700\u8981 CAS<br \/>\nbool dequeue(T&amp; val) {<br \/>\n    size_t h &#061; head_.load(std::memory_order_relaxed);  \/\/ \u53ea\u6709\u81ea\u5df1\u8bfb\u5199 head_<br \/>\n    size_t t &#061; tail_.load(std::memory_order_acquire);   \/\/ \u770b\u751f\u4ea7\u8005\u53d1\u5e03\u7684 tail<br \/>\n    if (h &#061;&#061; t) return false;  \/\/ \u7a7a<\/p>\n<p>    val &#061; buffer_[h &amp; mask_];<br \/>\n    head_.store(h &#043; 1, std::memory_order_release);     \/\/ \u53d1\u5e03\u7ed9\u751f\u4ea7\u8005&#xff08;\u8ba9\u5b83\u77e5\u9053\u8fd9\u4e2a slot \u56de\u6536\u4e86&#xff09;<br \/>\n    return true;<br \/>\n}<\/p>\n<p>\u6d88\u8d39\u7aef\u4e00\u4e2a CAS \u90fd\u6ca1\u6709\u3002\u6240\u6709\u7684\u7ade\u4e89\u538b\u529b\u90fd\u5728\u751f\u4ea7\u7aef\u7684\u00a0tail_\u00a0CAS \u4e0a&#xff0c;\u800c\u751f\u4ea7\u7aef\u53ea\u6709 IO \u7ebf\u7a0b\u5728\u7ade\u4e89\u2014\u2014\u6570\u91cf\u901a\u5e38\u5f88\u5c11&#xff08;1-4 \u4e2a&#xff09;\u3002<\/p>\n<h4>\u4ec0\u4e48\u65f6\u5019\u786e\u5b9e\u9700\u8981 MPMC<\/h4>\n<p>MPMC \u4e0d\u662f\u6ca1\u6709\u7528\u6b66\u4e4b\u5730\u3002\u5982\u679c\u4f60\u7684\u573a\u666f\u662f\u5de5\u4f5c\u7a83\u53d6&#xff08;work-stealing&#xff09;\u2014\u2014\u6bcf\u4e2a worker \u6709\u81ea\u5df1\u7684\u4efb\u52a1\u961f\u5217&#xff0c;\u4f46\u7a7a\u95f2\u65f6\u53ef\u4ee5\u4ece\u5176\u4ed6 worker \u7684\u961f\u5217\u91cc\u5077\u4efb\u52a1\u2014\u2014\u90a3 MPMC \u7684\u7075\u6d3b\u6027\u662f\u5fc5\u8981\u7684\u3002Go runtime \u7684 goroutine \u8c03\u5ea6\u5668\u548c Tokio \u7684 work-stealing scheduler \u90fd\u7528\u4e86\u8fd9\u79cd\u6a21\u5f0f\u3002<\/p>\n<p>\u4f46\u8981\u6ce8\u610f&#xff1a;\u771f\u6b63\u9ad8\u6027\u80fd\u7684 work-stealing \u5b9e\u73b0&#xff08;\u6bd4\u5982 Chase-Lev deque&#xff09;\u901a\u5e38\u4e0d\u662f\u4e00\u4e2a\u901a\u7528 MPMC \u961f\u5217&#xff0c;\u800c\u662f\u4e00\u4e2a SPSC &#043; \u539f\u5b50\u7a83\u53d6\u7aef\u7684\u6df7\u5408\u7ed3\u6784&#xff0c;\u628a\u5e38\u89c1\u8def\u5f84&#xff08;\u81ea\u5df1\u7684 push\/pop&#xff09;\u505a\u6210\u65e0\u7ade\u4e89\u7684&#xff0c;\u53ea\u6709\u7a83\u53d6\u8def\u5f84\u624d\u6709 CAS \u7ade\u4e89\u3002\u8fd9\u662f\u4e00\u4e2a\u6bd4\u901a\u7528 MPMC \u66f4\u7cbe\u7ec6\u7684\u8bbe\u8ba1\u3002<\/p>\n<hr \/>\n<h3>ABA \u4e0e\u751f\u547d\u5468\u671f&#xff1a;\u7f51\u7edc buffer \u590d\u7528\u573a\u666f\u4e0b\u6700\u9634\u9669\u7684\u9677\u9631<\/h3>\n<p>\u65e0\u9501\u961f\u5217\u5728\u7f51\u7edc\u670d\u52a1\u4e2d\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u751f\u547d\u5468\u671f\u9677\u9631&#xff1a;buffer \u590d\u7528\u3002\u7f51\u7edc\u6846\u67b6\u901a\u5e38\u9884\u5206\u914d\u4e00\u4e2a buffer pool&#xff0c;\u6536\u5305\u65f6\u4ece\u6c60\u4e2d\u53d6\u4e00\u4e2a buffer&#xff0c;\u5904\u7406\u5b8c\u540e\u5f52\u8fd8\u3002\u5982\u679c\u7528\u65e0\u9501\u6808\u6216\u65e0\u9501\u94fe\u8868\u7ba1\u7406\u8fd9\u4e2a pool&#xff0c;ABA \u95ee\u9898\u5c31\u4f1a\u4e25\u91cd\u5a01\u80c1\u6b63\u786e\u6027\u3002<\/p>\n<h4>ABA \u95ee\u9898\u7684\u7cbe\u786e\u5b9a\u4e49<\/h4>\n<p>ABA \u662f CAS \u64cd\u4f5c\u7684\u56fa\u6709\u9677\u9631&#xff1a;<\/p>\n<p>text<\/p>\n<p>\u65f6\u5e8f&#xff1a;<\/p>\n<p>\u7ebf\u7a0b A                          \u7ebf\u7a0b B<br \/>\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500                       \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\n\u8bfb\u53d6 head \u2192 \u6307\u9488 P&#xff08;\u6307\u5411 node X&#xff09;<br \/>\n                                pop(X)  \u2192  X \u88ab\u56de\u6536<br \/>\n                                          &#xff08;buffer \u5f52\u8fd8\u5230 pool&#xff09;<br \/>\n                                push(X) \u2192  X \u91cd\u65b0\u5165\u961f<br \/>\n                                          &#xff08;buffer \u88ab\u65b0\u8bf7\u6c42\u590d\u7528&#xff09;<br \/>\nCAS(head, P, P-&gt;next)<br \/>\n\u2192 CAS \u6210\u529f&#xff01;\u56e0\u4e3a head \u8fd8\u662f P<br \/>\n\u2192 \u4f46 P-&gt;next \u53ef\u80fd\u5df2\u7ecf\u53d8\u4e86&#xff08;X \u88ab\u590d\u7528\u540e next \u6307\u5411\u4e86\u522b\u7684 node&#xff09;<br \/>\n\u2192 \u961f\u5217\u7ed3\u6784\u88ab\u7834\u574f<\/p>\n<p>CAS \u53ea\u6bd4\u8f83\u503c&#xff08;\u6307\u9488\u5730\u5740&#xff09;&#xff0c;\u4e0d\u6bd4\u8f83\u8eab\u4efd&#xff08;\u8fd9\u4e2a node \u662f\u4e0d\u662f\u6211\u5f53\u521d\u770b\u5230\u7684\u90a3\u4e2a&#xff09;\u3002\u5f53 node X \u88ab\u56de\u6536\u53c8\u91cd\u65b0\u5206\u914d\u65f6&#xff0c;\u5b83\u7684\u5730\u5740\u6ca1\u53d8&#xff0c;\u4f46\u5185\u5bb9&#xff08;\u5305\u62ec\u00a0next\u00a0\u6307\u9488&#xff09;\u5df2\u7ecf\u88ab\u8986\u76d6\u4e86\u3002CAS \u770b\u5230\u5730\u5740\u6ca1\u53d8\u5c31\u8ba4\u4e3a\u201c\u6ca1\u4eba\u52a8\u8fc7\u201d&#xff0c;\u8fd9\u662f\u81f4\u547d\u7684\u8bef\u5224\u3002<\/p>\n<p>\u5728\u7f51\u7edc buffer \u590d\u7528\u573a\u666f\u4e0b&#xff0c;ABA \u5c24\u5176\u5371\u9669&#xff1a;<\/p>\n<li>\n<p>Buffer \u5730\u5740\u590d\u7528\u662f\u5e38\u6001\u3002\u9884\u5206\u914d\u7684 buffer pool \u672c\u5c31\u662f\u56fa\u5b9a\u5730\u5740\u96c6\u5408&#xff0c;pop \u51fa\u53bb\u7528\u5b8c\u53c8 push \u56de\u6765&#xff0c;\u5730\u5740\u5fc5\u7136\u91cd\u590d\u3002<\/p>\n<\/li>\n<li>\n<p>\u65f6\u95f4\u7a97\u53e3\u5f88\u5927\u3002\u4e00\u4e2a buffer \u53ef\u80fd\u5728\u7f51\u7edc IO\u3001\u534f\u8bae\u89e3\u6790\u3001\u4e1a\u52a1\u903b\u8f91\u3001\u5e8f\u5217\u5316\u6574\u4e2a\u94fe\u8def\u4e0a\u6d41\u8f6c&#xff0c;\u4ece pop \u5230 push \u56de\u6765\u53ef\u80fd\u8de8\u8d8a\u6570\u6beb\u79d2\u2014\u2014\u8db3\u591f\u5176\u4ed6\u7ebf\u7a0b\u5728\u961f\u5217\u4e0a\u505a\u591a\u6b21\u64cd\u4f5c\u3002<\/p>\n<\/li>\n<li>\n<p>\u540e\u679c\u4e0d\u662f crash&#xff0c;\u662f\u6570\u636e\u9519\u4e71\u3002\u961f\u5217\u7ed3\u6784\u88ab\u6084\u6084\u7834\u574f&#xff0c;\u540e\u7eed\u7684 pop \u53ef\u80fd\u8fd4\u56de\u9519\u8bef\u7684 buffer\u3001\u8df3\u8fc7 buffer\u3001\u751a\u81f3\u65e0\u9650\u5faa\u73af\u3002<\/p>\n<\/li>\n<h4>\u89e3\u51b3\u65b9\u6848\u4e00&#xff1a;Tagged Pointer&#xff08;\u7248\u672c\u53f7\u6807\u7b7e&#xff09;<\/h4>\n<p>\u6700\u7ecf\u5178\u7684 ABA \u89e3\u51b3\u65b9\u6848\u662f\u7ed9\u6307\u9488\u9644\u52a0\u4e00\u4e2a\u5355\u8c03\u9012\u589e\u7684\u7248\u672c\u53f7&#xff1a;<\/p>\n<p>C&#043;&#043;<\/p>\n<p>\/\/ C&#043;&#043;20, GCC 13\u3002Tagged pointer \u89e3\u51b3 ABA<br \/>\n\/\/ \u5229\u7528 x86-64 \u5730\u5740\u7a7a\u95f4\u53ea\u7528\u4f4e 48 \u4f4d&#xff0c;\u9ad8 16 \u4f4d\u53ef\u4ee5\u653e\u7248\u672c\u53f7<br \/>\n\/\/ \u6ce8\u610f&#xff1a;\u8fd9\u4e2a\u6280\u5de7\u5728\u6307\u9488\u538b\u7f29\u6216 5-level paging \u7684\u5185\u6838\u4e0a\u9700\u8981\u8c03\u6574<\/p>\n<p>struct TaggedPtr {<br \/>\n    uintptr_t ptr : 48;<br \/>\n    uintptr_t tag : 16;     \/\/ \u7248\u672c\u53f7&#xff0c;\u6bcf\u6b21 CAS \u6210\u529f\u540e &#043;1<br \/>\n};<\/p>\n<p>static_assert(sizeof(TaggedPtr) &#061;&#061; 8);<\/p>\n<p>\/\/ CAS \u65f6\u6bd4\u8f83\u6574\u4e2a 64 \u4f4d&#xff08;\u5305\u542b\u7248\u672c\u53f7&#xff09;&#xff0c;<br \/>\n\/\/ \u5373\u4f7f\u5730\u5740\u76f8\u540c\u3001\u7248\u672c\u53f7\u4e0d\u540c\u4e5f\u4f1a CAS \u5931\u8d25<br \/>\nstd::atomic&lt;TaggedPtr&gt; head_;<\/p>\n<p>bool pop(Node*&amp; result) {<br \/>\n    TaggedPtr old &#061; head_.load(std::memory_order_acquire);<br \/>\n    for (;;) {<br \/>\n        if (old.ptr &#061;&#061; 0) return false;<br \/>\n        Node* node &#061; reinterpret_cast&lt;Node*&gt;(old.ptr);<br \/>\n        TaggedPtr next{reinterpret_cast&lt;uintptr_t&gt;(node-&gt;next), old.tag &#043; 1};<br \/>\n        if (head_.compare_exchange_weak(old, next,<br \/>\n                std::memory_order_acq_rel, std::memory_order_acquire)) {<br \/>\n            result &#061; node;<br \/>\n            return true;<br \/>\n        }<br \/>\n        \/\/ old \u5df2\u88ab\u5237\u65b0&#xff0c;\u91cd\u8bd5<br \/>\n    }<br \/>\n}<\/p>\n<p>Tagged pointer \u5728\u5b9e\u8df5\u4e2d\u8db3\u591f\u597d\u2014\u201416 \u4f4d\u7248\u672c\u53f7\u610f\u5473\u7740\u8981 65536 \u6b21 ABA \u5de7\u5408\u624d\u4f1a\u8bef\u5224\u3002\u4f46\u5b83\u6709\u4e24\u4e2a\u9650\u5236&#xff1a;\u4e00\u662f\u4f9d\u8d56\u5e73\u53f0\u7279\u5b9a\u7684\u6307\u9488\u5e03\u5c40&#xff08;x86-64 \u7684 48 \u4f4d\u5730\u5740\u7a7a\u95f4&#xff09;&#xff0c;\u53ef\u79fb\u690d\u6027\u5dee&#xff1b;\u4e8c\u662f\u5728 AArch64 \u4e0a\u4e5f\u53ef\u4ee5\u5229\u7528\u9ad8\u4f4d&#xff0c;\u4f46 Top Byte Ignore&#xff08;TBI&#xff09;\u548c Memory Tagging Extension&#xff08;MTE&#xff09;\u53ef\u80fd\u5360\u7528\u8fd9\u4e9b\u4f4d\u3002<\/p>\n<h4>\u89e3\u51b3\u65b9\u6848\u4e8c&#xff1a;Hazard Pointer<\/h4>\n<p>Hazard pointer \u662f Maged M. Michael \u5728 2004 \u5e74\u63d0\u51fa\u7684\u5b89\u5168\u56de\u6536\u65b9\u6848&#xff0c;\u5df2\u88ab\u63a5\u7eb3\u4e3a C&#043;&#043;26 \u6807\u51c6&#xff08;&lt;hazard_pointer&gt;, P2530&#xff09;\u3002\u6838\u5fc3\u601d\u60f3&#xff1a;**\u7ebf\u7a0b\u5728\u8bbf\u95ee\u4e00\u4e2a\u5171\u4eab node \u4e4b\u524d&#xff0c;\u5148\u628a\u8fd9\u4e2a node \u7684\u5730\u5740\u201c\u516c\u544a\u201d\u5728\u81ea\u5df1\u7684 hazard pointer slot \u91cc&#xff1b;\u5176\u4ed6\u7ebf\u7a0b\u5728\u56de\u6536 node \u524d&#xff0c;\u5fc5\u987b\u626b\u63cf\u6240\u6709\u7ebf\u7a0b\u7684 hazard pointer&#xff0c;\u53ea\u6709\u6ca1\u4eba\u4fdd\u62a4\u7684 node \u624d\u80fd\u771f\u6b63\u91ca\u653e\u3002**<\/p>\n<p>C&#043;&#043;<\/p>\n<p>\/\/ C&#043;&#043;26 hazard_pointer \u6982\u5ff5\u793a\u610f&#xff08;API \u4ee5 P2530R4 \u4e3a\u51c6&#xff09;<br \/>\n\/\/ \u7b80\u5316\u5c55\u793a\u6838\u5fc3\u6d41\u7a0b<\/p>\n<p>void consumer_thread(LockFreeStack&amp; stack) {<br \/>\n    auto hp &#061; std::make_hazard_pointer();   \/\/ \u83b7\u53d6\u4e00\u4e2a hazard pointer slot<\/p>\n<p>    for (;;) {<br \/>\n        Node* ptr;<br \/>\n        do {<br \/>\n            ptr &#061; stack.head_.load(std::memory_order_acquire);<br \/>\n            if (!ptr) break;<br \/>\n            hp.protect(ptr);               \/\/ \u516c\u544a&#xff1a;\u6211\u5728\u7528\u8fd9\u4e2a node<br \/>\n            \/\/ \u91cd\u8bfb head&#xff0c;\u786e\u8ba4\u6ca1\u6709\u5728 protect \u4e4b\u524d\u5c31\u88ab\u522b\u4eba\u6539\u4e86<br \/>\n        } while (ptr !&#061; stack.head_.load(std::memory_order_acquire));<\/p>\n<p>        if (!ptr) continue;<\/p>\n<p>        \/\/ \u73b0\u5728 ptr \u53d7\u4fdd\u62a4&#xff0c;\u5373\u4f7f\u522b\u7684\u7ebf\u7a0b pop \u4e86\u5b83\u4e5f\u4e0d\u4f1a\u91ca\u653e<br \/>\n        \/\/ &#8230; \u4f7f\u7528 ptr &#8230;<\/p>\n<p>        hp.reset_protection();             \/\/ \u7528\u5b8c\u4e86&#xff0c;\u53d6\u6d88\u4fdd\u62a4<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u56de\u6536\u7aef&#xff1a;\u628a retired \u7684 node \u653e\u8fdb\u5f85\u56de\u6536\u5217\u8868<br \/>\n\/\/ \u8fbe\u5230\u9608\u503c\u540e\u626b\u63cf\u6240\u6709 hazard pointer&#xff0c;\u53ea\u91ca\u653e\u6ca1\u4eba\u4fdd\u62a4\u7684<br \/>\nvoid retire_node(Node* node) {<br \/>\n    node-&gt;retire();   \/\/ C&#043;&#043;26: \u52a0\u5165\u7ebf\u7a0b\u672c\u5730\u7684 retired list<br \/>\n    \/\/ \u5f53 retired list \u8db3\u591f\u957f\u65f6\u81ea\u52a8\u89e6\u53d1 scan &#043; reclaim<br \/>\n}<\/p>\n<p>Hazard pointer \u7684\u4ee3\u4ef7\u662f\u6bcf\u6b21\u8bbf\u95ee\u90fd\u9700\u8981\u4e00\u4e2a atomic store&#xff08;protect&#xff09;\u548c\u4e00\u4e2a atomic load&#xff08;\u9a8c\u8bc1&#xff09;&#xff0c;\u52a0\u4e0a\u56de\u6536\u65f6\u7684\u5168\u5c40\u626b\u63cf\u3002\u5728\u8bfb\u5bc6\u96c6\u573a\u666f\u4e0b\u8fd9\u4e2a\u5f00\u9500\u4e0d\u7b97\u5c0f\u2014\u2014\u6bcf\u6b21 protect \u81f3\u5c11\u4e00\u6b21 release store &#043; \u4e00\u6b21 acquire load\u3002<\/p>\n<h4>\u89e3\u51b3\u65b9\u6848\u4e09&#xff1a;Epoch-Based Reclamation&#xff08;EBR&#xff09;<\/h4>\n<p>EBR \u662f\u4e00\u79cd\u66f4\u7c97\u7c92\u5ea6\u7684\u65b9\u6848&#xff1a;\u7ef4\u62a4\u4e00\u4e2a\u5168\u5c40 epoch \u8ba1\u6570\u5668&#xff08;\u901a\u5e38\u662f 0\u30011\u30012 \u4e09\u4e2a\u503c\u5faa\u73af&#xff09;&#xff0c;\u6bcf\u4e2a\u7ebf\u7a0b\u8fdb\u5165\u201c\u4e34\u754c\u533a\u201d\u65f6\u8bb0\u5f55\u5f53\u524d epoch&#xff0c;\u9000\u51fa\u4e34\u754c\u533a\u65f6\u6e05\u9664\u3002\u56de\u6536\u903b\u8f91\u53ea\u5728\u786e\u8ba4\u6240\u6709\u7ebf\u7a0b\u90fd\u5df2\u7ecf\u79bb\u5f00\u65e7 epoch \u65f6\u624d\u91ca\u653e\u90a3\u4e2a epoch \u7684 retired node\u3002<\/p>\n<p>C&#043;&#043;<\/p>\n<p>\/\/ Epoch-based reclamation \u6982\u5ff5\u793a\u610f<br \/>\n\/\/ C&#043;&#043;20, GCC 13<\/p>\n<p>class EpochGuard {<br \/>\n    thread_local static size_t local_epoch_;<br \/>\n    static std::atomic&lt;size_t&gt; global_epoch_;<br \/>\n    static thread_local std::vector&lt;Node*&gt; retired_[3];  \/\/ \u4e09\u4e2a epoch \u7684\u5f85\u56de\u6536\u5217\u8868<\/p>\n<p>public:<br \/>\n    \/\/ \u8fdb\u5165\u4e34\u754c\u533a<br \/>\n    void enter() {<br \/>\n        local_epoch_ &#061; global_epoch_.load(std::memory_order_acquire);<br \/>\n        \/\/ \u6ce8\u610f&#xff1a;\u8fd9\u91cc\u9700\u8981\u4e00\u4e2a fence \u4fdd\u8bc1 local_epoch_ \u7684\u5199<br \/>\n        \/\/ \u5bf9 epoch advancement \u903b\u8f91\u53ef\u89c1<br \/>\n    }<\/p>\n<p>    \/\/ \u79bb\u5f00\u4e34\u754c\u533a<br \/>\n    void leave() {<br \/>\n        \/\/ \u6807\u8bb0\u672c\u7ebf\u7a0b\u5df2\u7ecf\u4e0d\u5728\u4e34\u754c\u533a<br \/>\n        \/\/ &#xff08;\u5b9e\u73b0\u4e0a\u901a\u5e38\u662f\u628a local_epoch_ \u8bbe\u4e3a\u7279\u6b8a\u503c INACTIVE&#xff09;<br \/>\n    }<\/p>\n<p>    \/\/ \u63a8\u8fdb epoch&#xff1a;\u5f53\u6240\u6709\u7ebf\u7a0b\u90fd\u5df2\u79bb\u5f00 old_epoch \u65f6\u624d\u80fd\u63a8\u8fdb<br \/>\n    static void try_advance() {<br \/>\n        size_t e &#061; global_epoch_.load(std::memory_order_relaxed);<br \/>\n        \/\/ \u626b\u63cf\u6240\u6709\u7ebf\u7a0b\u7684 local_epoch_<br \/>\n        \/\/ \u5982\u679c\u6240\u6709\u7ebf\u7a0b\u8981\u4e48\u5728\u5f53\u524d epoch&#xff0c;\u8981\u4e48 INACTIVE<br \/>\n        \/\/ \u2192 \u63a8\u8fdb epoch&#xff0c;\u56de\u6536 (e &#8211; 2) \u7684 retired list<br \/>\n    }<\/p>\n<p>    void retire(Node* node) {<br \/>\n        retired_[local_epoch_ % 3].push_back(node);<br \/>\n        if (retired_[local_epoch_ % 3].size() &gt; threshold) {<br \/>\n            try_advance();<br \/>\n        }<br \/>\n    }<br \/>\n};<\/p>\n<p>EBR \u7684\u8bfb\u7aef\u5f00\u9500\u6781\u4f4e\u2014\u2014\u8fdb\u5165\u4e34\u754c\u533a\u53ea\u662f\u4e00\u6b21 atomic load &#043; \u4e00\u6b21\u7ebf\u7a0b\u5c40\u90e8\u5199&#xff0c;\u6ca1\u6709 per-object \u7684 atomic store\u3002\u4f46\u5b83\u6709\u4e00\u4e2a\u81f4\u547d\u5f31\u70b9&#xff1a;\u5982\u679c\u4efb\u4f55\u4e00\u4e2a\u7ebf\u7a0b\u5728\u4e34\u754c\u533a\u5185\u88ab\u963b\u585e&#xff08;\u6bd4\u5982\u9677\u5165 page fault\u3001\u88ab\u8c03\u5ea6\u5668\u62a2\u5360\u3001\u6216\u8005\u5f00\u53d1\u8005\u4e0d\u5c0f\u5fc3\u5728\u4e34\u754c\u533a\u5185\u505a\u4e86\u963b\u585e IO&#xff09;&#xff0c;\u5168\u5c40 epoch \u5c31\u63a8\u4e0d\u52a8&#xff0c;\u6240\u6709 retired node \u90fd\u65e0\u6cd5\u56de\u6536&#xff0c;\u5185\u5b58\u65e0\u9650\u589e\u957f\u3002<\/p>\n<h4>\u7f51\u7edc buffer \u573a\u666f\u8be5\u9009\u54ea\u4e2a<\/h4>\n<p>\u5bf9\u4e8e\u9884\u5206\u914d buffer pool &#043; \u9ad8\u9891\u590d\u7528\u7684\u7f51\u7edc\u573a\u666f&#xff0c;\u6211\u503e\u5411\u4e8e&#xff1a;<\/p>\n<li>\n<p>\u5982\u679c\u7528 SPSC ring buffer \u505a\u961f\u5217&#xff08;\u540e\u6587\u4f1a\u5b9e\u73b0\u4e00\u4e2a&#xff09;&#xff1a;SPSC \u5929\u7136\u6ca1\u6709 ABA \u95ee\u9898&#xff0c;\u56e0\u4e3a buffer \u5728 ring \u4e2d\u7684\u4f4d\u7f6e\u662f\u56fa\u5b9a\u7684&#xff0c;\u4e0d\u6d89\u53ca\u6307\u9488\u94fe\u8868\u3002\u8fd9\u662f\u6700\u5e72\u51c0\u7684\u65b9\u6848\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u5fc5\u987b\u7528\u94fe\u8868\u5f0f\u65e0\u9501\u6808\u7ba1\u7406 buffer pool&#xff1a;\u7528 tagged pointer\u3002\u7f51\u7edc buffer \u7684\u5730\u5740\u7a7a\u95f4\u901a\u5e38\u662f\u9884\u5206\u914d\u7684\u56fa\u5b9a\u5927\u5c0f&#xff0c;16 \u4f4d\u7248\u672c\u53f7\u8db3\u591f\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c buffer \u7684\u751f\u547d\u5468\u671f\u8de8\u8d8a\u591a\u4e2a\u9636\u6bb5&#xff08;\u6536\u5305 \u2192 \u89e3\u6790 \u2192 \u5904\u7406 \u2192 \u5f52\u8fd8&#xff09;&#xff1a;\u7528 hazard pointer\u3002EBR \u5728\u7f51\u7edc\u573a\u666f\u4e0b\u98ce\u9669\u5927\u2014\u2014worker \u7ebf\u7a0b\u53ef\u80fd\u5728\u5904\u7406\u67d0\u4e2a\u8bf7\u6c42\u65f6\u505a\u4e86\u4e00\u6b21 DNS \u67e5\u8be2\u6216\u8fdc\u7a0b\u8c03\u7528&#xff0c;\u963b\u585e\u4e86\u51e0\u767e\u6beb\u79d2&#xff0c;\u6574\u4e2a epoch \u63a8\u4e0d\u52a8\u3002<\/p>\n<\/li>\n<p>\u4f46\u5148\u522b\u6025\u7740\u9009\u65b9\u6848\u3002\u771f\u6b63\u7684\u5de5\u7a0b\u5efa\u8bae\u662f&#xff1a;\u5148\u770b\u4f60\u5230\u5e95\u9700\u4e0d\u9700\u8981\u94fe\u8868\u3002\u5982\u679c\u961f\u5217\u662f bounded \u7684&#xff08;\u7f51\u7edc\u573a\u666f\u51e0\u4e4e\u90fd\u662f&#xff09;&#xff0c;\u7528 ring buffer \u5c31\u5b8c\u5168\u907f\u5f00\u4e86 ABA \u548c\u5185\u5b58\u56de\u6536\u7684\u5168\u90e8\u590d\u6742\u5ea6\u3002<\/p>\n<hr \/>\n<h3>\u4e00\u4e2a\u53ef\u79fb\u690d SPSC Ring \u7684\u5b8c\u6574\u5b9e\u73b0<\/h3>\n<p>\u5230\u8fd9\u91cc\u8be5\u7ed9\u5b8c\u6574\u7684\u4ee3\u7801\u4e86\u3002\u8fd9\u662f\u4e00\u4e2a\u53ef\u79fb\u690d\u7684 SPSC ring buffer&#xff0c;\u5173\u952e\u8bbe\u8ba1\u70b9&#xff1a;\u6b63\u786e\u7684 acquire\/release \u6620\u5c04\u3001cache line padding \u907f\u514d false sharing\u3001power-of-two \u5bb9\u91cf\u7528\u4f4d\u8fd0\u7b97\u4ee3\u66ff\u53d6\u6a21\u3001\u672c\u5730\u7f13\u5b58\u5bf9\u7aef\u6307\u9488\u51cf\u5c11\u8de8\u6838 cache line \u8bfb\u53d6\u3002<\/p>\n<p>C&#043;&#043;<\/p>\n<p>\/\/ spsc_ring.hpp<br \/>\n\/\/ C&#043;&#043;20, GCC 13 \/ Clang 17, \u53ef\u79fb\u690d\u5230 x86-64 \u548c AArch64<br \/>\n\/\/ \u53c2\u8003 rigtorp\/SPSCQueue \u7684\u6838\u5fc3\u601d\u8def&#xff0c;\u505a\u4e86\u7b80\u5316\u548c\u6ce8\u91ca<br \/>\n\/\/ License: MIT (\u793a\u4f8b\u4ee3\u7801)<\/p>\n<p>#pragma once<br \/>\n#include &lt;atomic&gt;<br \/>\n#include &lt;cassert&gt;<br \/>\n#include &lt;cstddef&gt;<br \/>\n#include &lt;new&gt;       \/\/ std::hardware_destructive_interference_size<br \/>\n#include &lt;type_traits&gt;<\/p>\n<p>\/\/ cache line \u5927\u5c0f&#xff1a;C&#043;&#043;17 \u5f15\u5165\u4e86 hardware_destructive_interference_size&#xff0c;<br \/>\n\/\/ \u4f46\u622a\u81f3 GCC 13 \u548c Clang 17 \u5f88\u591a\u5e73\u53f0\u4e0a\u5b83\u8fd8\u662f constexpr \u672a\u5b9a\u4e49\u6216\u9700\u8981<br \/>\n\/\/ -Wno-interference-size\u3002\u8fd9\u91cc\u7528 64 \u4f5c\u4e3a\u4fdd\u5b88\u503c&#xff08;\u8986\u76d6 x86 \u548c ARM \u4e3b\u6d41 CPU&#xff09;<br \/>\n#ifndef CACHE_LINE_SIZE<br \/>\n#define CACHE_LINE_SIZE 64<br \/>\n#endif<\/p>\n<p>template&lt;typename T&gt;<br \/>\nclass SPSCRing {<br \/>\n    static_assert(std::is_nothrow_move_constructible_v&lt;T&gt; ||<br \/>\n                  std::is_nothrow_copy_constructible_v&lt;T&gt;,<br \/>\n                  &#034;T must be nothrow move or copy constructible&#034;);<\/p>\n<p>public:<br \/>\n    \/\/ capacity \u5fc5\u987b\u662f 2 \u7684\u5e42&#xff0c;\u65b9\u4fbf\u7528 &amp; (capacity_ &#8211; 1) \u4ee3\u66ff % capacity_<br \/>\n    explicit SPSCRing(size_t capacity)<br \/>\n        : capacity_(capacity)<br \/>\n        , mask_(capacity &#8211; 1)<br \/>\n        , buffer_(static_cast&lt;T*&gt;(<br \/>\n              ::operator new(sizeof(T) * capacity, std::align_val_t{CACHE_LINE_SIZE})))<br \/>\n    {<br \/>\n        assert(capacity &gt;&#061; 2 &amp;&amp; (capacity &amp; (capacity &#8211; 1)) &#061;&#061; 0<br \/>\n               &amp;&amp; &#034;capacity must be a power of two&#034;);<br \/>\n    }<\/p>\n<p>    ~SPSCRing() {<br \/>\n        \/\/ \u6790\u6784\u672a\u6d88\u8d39\u7684\u5143\u7d20<br \/>\n        size_t h &#061; head_.load(std::memory_order_relaxed);<br \/>\n        size_t t &#061; tail_.load(std::memory_order_relaxed);<br \/>\n        while (h !&#061; t) {<br \/>\n            buffer_[h &amp; mask_].~T();<br \/>\n            &#043;&#043;h;<br \/>\n        }<br \/>\n        ::operator delete(buffer_, std::align_val_t{CACHE_LINE_SIZE});<br \/>\n    }<\/p>\n<p>    \/\/ \u7981\u6b62\u62f7\u8d1d\u548c\u79fb\u52a8&#xff08;\u6307\u9488\u548c\u539f\u5b50\u72b6\u6001\u4e0d\u53ef\u5b89\u5168\u8f6c\u79fb&#xff09;<br \/>\n    SPSCRing(const SPSCRing&amp;) &#061; delete;<br \/>\n    SPSCRing&amp; operator&#061;(const SPSCRing&amp;) &#061; delete;<\/p>\n<p>    \/\/ &#061;&#061;&#061; \u751f\u4ea7\u8005\u7aef &#061;&#061;&#061;<br \/>\n    \/\/ \u8c03\u7528\u65b9\u4fdd\u8bc1&#xff1a;\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u8c03\u7528 push<br \/>\n    template&lt;typename U&gt;<br \/>\n    bool push(U&amp;&amp; val) {<br \/>\n        size_t t &#061; tail_.load(std::memory_order_relaxed);<\/p>\n<p>        \/\/ \u5148\u67e5\u672c\u5730\u7f13\u5b58\u7684 head&#xff0c;\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8de8\u6838 cache line \u8bfb\u53d6<br \/>\n        if (t &#8211; cached_head_ &gt;&#061; capacity_) {<br \/>\n            \/\/ \u672c\u5730\u7f13\u5b58\u8bf4&#034;\u6ee1\u4e86&#034;&#xff0c;\u4f46\u53ef\u80fd\u6d88\u8d39\u8005\u5df2\u7ecf\u63a8\u8fdb\u4e86 head<br \/>\n            cached_head_ &#061; head_.load(std::memory_order_acquire);<br \/>\n            if (t &#8211; cached_head_ &gt;&#061; capacity_) {<br \/>\n                return false;  \/\/ \u771f\u7684\u6ee1\u4e86<br \/>\n            }<br \/>\n        }<\/p>\n<p>        \/\/ \u6784\u9020\u5143\u7d20\u5230 buffer \u4e2d<br \/>\n        new (&amp;buffer_[t &amp; mask_]) T(std::forward&lt;U&gt;(val));<\/p>\n<p>        \/\/ \u5173\u952e&#xff1a;release store \u53d1\u5e03 tail<br \/>\n        \/\/ \u4fdd\u8bc1\u4e0a\u9762\u7684\u6784\u9020\u64cd\u4f5c\u5728\u6b64 store \u4e4b\u524d\u5bf9\u6d88\u8d39\u8005\u53ef\u89c1<br \/>\n        tail_.store(t &#043; 1, std::memory_order_release);<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    \/\/ &#061;&#061;&#061; \u6d88\u8d39\u8005\u7aef &#061;&#061;&#061;<br \/>\n    \/\/ \u8c03\u7528\u65b9\u4fdd\u8bc1&#xff1a;\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u8c03\u7528 pop<br \/>\n    bool pop(T&amp; val) {<br \/>\n        size_t h &#061; head_.load(std::memory_order_relaxed);<\/p>\n<p>        \/\/ \u5148\u67e5\u672c\u5730\u7f13\u5b58\u7684 tail<br \/>\n        if (h &#061;&#061; cached_tail_) {<br \/>\n            cached_tail_ &#061; tail_.load(std::memory_order_acquire);<br \/>\n            if (h &#061;&#061; cached_tail_) {<br \/>\n                return false;  \/\/ \u771f\u7684\u7a7a<br \/>\n            }<br \/>\n        }<\/p>\n<p>        \/\/ \u8bfb\u53d6\u5e76\u79fb\u52a8\u5143\u7d20<br \/>\n        val &#061; std::move(buffer_[h &amp; mask_]);<br \/>\n        buffer_[h &amp; mask_].~T();<\/p>\n<p>        \/\/ \u5173\u952e&#xff1a;release store \u53d1\u5e03 head<br \/>\n        \/\/ \u4fdd\u8bc1\u4e0a\u9762\u7684\u6790\u6784\u64cd\u4f5c\u5728\u6b64 store \u4e4b\u524d\u5b8c\u6210<br \/>\n        \/\/ &#xff08;\u5426\u5219\u751f\u4ea7\u8005\u53ef\u80fd\u5728\u6d88\u8d39\u8005\u8fd8\u6ca1\u8bfb\u5b8c\u65f6\u5c31\u8986\u5199\u8fd9\u4e2a slot&#xff09;<br \/>\n        head_.store(h &#043; 1, std::memory_order_release);<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    size_t size_approx() const {<br \/>\n        \/\/ \u8fd1\u4f3c\u5927\u5c0f&#xff0c;\u4e0d\u4fdd\u8bc1\u7cbe\u786e&#xff08;head \u548c tail \u7684\u8bfb\u53d6\u4e0d\u662f\u539f\u5b50\u7684\u4e00\u5bf9&#xff09;<br \/>\n        return tail_.load(std::memory_order_relaxed)<br \/>\n             &#8211; head_.load(std::memory_order_relaxed);<br \/>\n    }<\/p>\n<p>private:<br \/>\n    const size_t capacity_;<br \/>\n    const size_t mask_;<br \/>\n    T* const buffer_;<\/p>\n<p>    \/\/ \u2500\u2500\u2500 Cache Line \u9694\u79bb \u2500\u2500\u2500<br \/>\n    \/\/ head_ \u53ea\u88ab\u6d88\u8d39\u8005\u5199\u3001\u88ab\u751f\u4ea7\u8005\u8bfb&#xff08;\u901a\u8fc7 cached_head_ \u51cf\u5c11\u9891\u7387&#xff09;<br \/>\n    \/\/ tail_ \u53ea\u88ab\u751f\u4ea7\u8005\u5199\u3001\u88ab\u6d88\u8d39\u8005\u8bfb&#xff08;\u901a\u8fc7 cached_tail_ \u51cf\u5c11\u9891\u7387&#xff09;<br \/>\n    \/\/ \u628a\u5b83\u4eec\u653e\u5728\u4e0d\u540c\u7684 cache line \u4e0a&#xff0c;\u907f\u514d false sharing<\/p>\n<p>    \/\/ \u751f\u4ea7\u8005\u72ec\u5360\u7684 cache line<br \/>\n    alignas(CACHE_LINE_SIZE) std::atomic&lt;size_t&gt; tail_{0};<br \/>\n    size_t cached_head_{0};    \/\/ \u751f\u4ea7\u8005\u672c\u5730\u7f13\u5b58\u7684 head \u503c<\/p>\n<p>    \/\/ \u6d88\u8d39\u8005\u72ec\u5360\u7684 cache line<br \/>\n    alignas(CACHE_LINE_SIZE) std::atomic&lt;size_t&gt; head_{0};<br \/>\n    size_t cached_tail_{0};    \/\/ \u6d88\u8d39\u8005\u672c\u5730\u7f13\u5b58\u7684 tail \u503c<\/p>\n<p>    \/\/ padding \u5230\u4e0b\u4e00\u4e2a cache line \u8fb9\u754c&#xff0c;\u9632\u6b62 buffer \u540e\u9762\u7684\u5176\u4ed6\u5bf9\u8c61\u8bef\u5165<br \/>\n    char pad_[CACHE_LINE_SIZE &#8211; sizeof(std::atomic&lt;size_t&gt;) &#8211; sizeof(size_t)];<br \/>\n};<\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u6709\u51e0\u4e2a\u5173\u952e\u8bbe\u8ba1\u51b3\u7b56\u503c\u5f97\u9010\u4e00\u62c6\u89e3\u3002<\/p>\n<h4>\u51b3\u7b56 1&#xff1a;\u4e3a\u4ec0\u4e48 capacity \u5fc5\u987b\u662f 2 \u7684\u5e42<\/h4>\n<p>buffer_[t % capacity]\u00a0\u7684\u53d6\u6a21\u8fd0\u7b97\u5728 x86 \u4e0a\u7f16\u8bd1\u6210\u00a0div\u00a0\u6216\u00a0idiv&#xff0c;\u5ef6\u8fdf\u7ea6 30-40 \u4e2a\u5468\u671f\u3002buffer_[t &amp; mask_]\u00a0\u7684\u4f4d\u4e0e\u8fd0\u7b97\u7f16\u8bd1\u6210\u00a0and&#xff0c;\u5ef6\u8fdf 1 \u4e2a\u5468\u671f\u3002\u5728\u6bcf\u79d2\u51e0\u4ebf\u6b21\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c\u4e2d&#xff0c;\u8fd9\u4e2a\u5dee\u5f02\u662f\u663e\u8457\u7684\u3002rigtorp\/SPSCQueue \u548c Folly \u7684 ProducerConsumerQueue \u90fd\u5f3a\u5236 power-of-two \u5bb9\u91cf\u3002<\/p>\n<h4>\u51b3\u7b56 2&#xff1a;cache line padding \u7684\u7cbe\u786e\u5e03\u5c40<\/h4>\n<p>false sharing \u662f SPSC ring \u6700\u5927\u7684\u6027\u80fd\u6740\u624b\u3002\u5982\u679c\u00a0head_\u00a0\u548c\u00a0tail_\u00a0\u5728\u540c\u4e00\u6761 cache line \u4e0a&#xff0c;\u6d88\u8d39\u8005\u6bcf\u6b21\u66f4\u65b0\u00a0head_\u00a0\u90fd\u4f1a\u8ba9\u751f\u4ea7\u8005\u6838\u5fc3\u7684\u00a0tail_\u00a0\u6240\u5728 cache line \u5931\u6548&#xff0c;\u53cd\u4e4b\u4ea6\u7136\u3002\u4e24\u4e2a\u6838\u5fc3\u4ea4\u66ff\u5730\u628a\u540c\u4e00\u6761 cache line \u62c9\u6765\u62c9\u53bb\u2014\u2014\u8fd9\u5c31\u662f false sharing&#xff0c;\u4e5f\u53eb&#034;cache line ping-pong&#034;\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848\u662f\u7528\u00a0alignas(CACHE_LINE_SIZE)\u00a0\u628a\u00a0head_\u00a0\u548c\u00a0tail_\u00a0\u653e\u5728\u4e0d\u540c\u7684 cache line \u4e0a\u3002\u4f46\u5149\u9694\u79bb\u8fd9\u4e24\u4e2a\u539f\u5b50\u53d8\u91cf\u8fd8\u4e0d\u591f\u2014\u2014cached_head_\u00a0\u548c\u00a0cached_tail_\u00a0\u4e5f\u8981\u8ddf\u5bf9\u5e94\u7684\u539f\u5b50\u53d8\u91cf\u653e\u5728\u540c\u4e00\u6761 cache line \u4e0a&#xff1a;<\/p>\n<p>text<\/p>\n<p>Cache Line 0:  [tail_       | cached_head_ | padding&#8230;]  \u2190 \u751f\u4ea7\u8005\u72ec\u5360<br \/>\nCache Line 1:  [head_       | cached_tail_ | padding&#8230;]  \u2190 \u6d88\u8d39\u8005\u72ec\u5360<\/p>\n<p>cached_head_\u00a0\u53ea\u88ab\u751f\u4ea7\u8005\u8bfb\u5199&#xff0c;\u6240\u4ee5\u5b83\u8ddf\u00a0tail_&#xff08;\u4e5f\u53ea\u88ab\u751f\u4ea7\u8005\u5199&#xff09;\u653e\u540c\u4e00\u6761 cache line \u6ca1\u95ee\u9898\u2014\u2014\u90fd\u662f\u540c\u4e00\u4e2a\u6838\u5fc3\u5728\u64cd\u4f5c&#xff0c;\u4e0d\u4f1a ping-pong\u3002cached_tail_\u00a0\u540c\u7406\u3002<\/p>\n<h4>\u51b3\u7b56 3&#xff1a;\u672c\u5730\u7f13\u5b58\u5bf9\u7aef\u6307\u9488&#xff08;\u6700\u5173\u952e\u7684\u4f18\u5316&#xff09;<\/h4>\n<p>\u8fd9\u662f rigtorp\/SPSCQueue \u76f8\u5bf9\u4e8e\u6734\u7d20\u5b9e\u73b0\u6027\u80fd\u63d0\u5347\u6700\u5927\u7684\u4e00\u4e2a\u6280\u5de7\u3002<\/p>\n<p>\u6734\u7d20\u5b9e\u73b0\u4e2d&#xff0c;\u751f\u4ea7\u8005\u6bcf\u6b21 push \u90fd\u8981\u505a\u00a0head_.load(acquire)\u00a0\u6765\u68c0\u67e5\u961f\u5217\u662f\u5426\u6ee1&#xff1a;\u8fd9\u610f\u5473\u7740**\u6bcf\u6b21 push \u90fd\u8981\u8de8\u6838\u8bfb\u53d6\u6d88\u8d39\u8005\u7684\u00a0head_&#xff0c;\u89e6\u53d1\u4e00\u6b21 cache line \u4f20\u8f93**\u3002\u5982\u679c\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u5728\u4e0d\u540c\u7684\u6838\u5fc3\u4e0a&#xff08;\u901a\u5e38\u5982\u6b64&#xff09;&#xff0c;\u8fd9\u4e00\u6b21\u8de8\u6838\u8bfb\u53d6\u7684\u5ef6\u8fdf\u662f 40-100 \u4e2a\u5468\u671f\u3002<\/p>\n<p>\u672c\u5730\u7f13\u5b58\u7684\u505a\u6cd5\u662f&#xff1a;\u751f\u4ea7\u8005\u628a\u4e0a\u6b21\u8bfb\u5230\u7684\u00a0head_\u00a0\u503c\u5b58\u5728\u81ea\u5df1\u7684\u00a0cached_head_\u00a0\u91cc&#xff08;\u4e0d\u662f atomic&#xff0c;\u4e0d\u9700\u8981\u8de8\u6838\u53ef\u89c1&#xff09;\u3002push \u65f6\u5148\u7528\u00a0cached_head_\u00a0\u68c0\u67e5\u2014\u2014\u5982\u679c\u6309\u7f13\u5b58\u7684\u503c\u961f\u5217\u8fd8\u6ca1\u6ee1&#xff0c;\u76f4\u63a5\u8df3\u8fc7\u8de8\u6838\u8bfb\u53d6\u3002\u53ea\u6709\u5f53\u7f13\u5b58\u503c\u663e\u793a\u201c\u6ee1\u4e86\u201d\u65f6&#xff0c;\u624d\u771f\u6b63\u53bb\u8bfb\u4e00\u6b21\u00a0head_.load(acquire)&#xff0c;\u66f4\u65b0\u7f13\u5b58\u3002<\/p>\n<p>\u8fd9\u610f\u5473\u7740&#xff1a;**\u5982\u679c\u961f\u5217\u4e0d\u5e38\u6ee1&#xff08;\u7f51\u7edc\u573a\u666f\u4e2d\u901a\u5e38\u5982\u6b64&#xff09;&#xff0c;\u751f\u4ea7\u8005\u53ef\u80fd\u8fde\u7eed push \u51e0\u5343\u6b21\u90fd\u4e0d\u9700\u8981\u8bfb\u4e00\u6b21\u00a0head_\u3002** \u8de8\u6838 cache line \u4f20\u8f93\u4ece\u201c\u6bcf\u6b21 push \u4e00\u6b21\u201d\u964d\u5230\u4e86\u201c\u961f\u5217\u5feb\u6ee1\u65f6\u624d\u4e00\u6b21\u201d\u3002<\/p>\n<p>\u6d88\u8d39\u8005\u7aef\u5bf9\u00a0tail_\u00a0\u505a\u540c\u6837\u7684\u7f13\u5b58\u3002<\/p>\n<p>\u6211\u7528\u00a0perf stat\u00a0\u5bf9\u6bd4\u8fc7\u6709\u65e0\u672c\u5730\u7f13\u5b58\u7684\u7248\u672c&#xff08;GCC 13, -O2, Intel Xeon Gold 6348&#xff0c;\u4e24\u4e2a\u7ebf\u7a0b\u5206\u522b\u7ed1\u5b9a\u5230\u4e0d\u540c\u6838\u5fc3&#xff0c;\u961f\u5217\u5bb9\u91cf 65536&#xff0c;\u8fde\u7eed push\/pop 1 \u4ebf\u6761 64 \u5b57\u8282\u6d88\u606f&#xff09;&#xff1a;<\/p>\n<p>text<\/p>\n<p>                     \u65e0\u7f13\u5b58\u7248        \u6709\u7f13\u5b58\u7248<br \/>\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\nL1-dcache-load-misses  ~45M           ~2.1M<br \/>\nLLC-load-misses        ~12M           ~0.4M<br \/>\nops\/sec                ~180M          ~320M<\/p>\n<p>cache miss \u6570\u91cf\u964d\u4e86\u4e00\u4e2a\u6570\u91cf\u7ea7\u4ee5\u4e0a&#xff0c;\u541e\u5410\u51e0\u4e4e\u7ffb\u500d\u3002<\/p>\n<h4>\u51b3\u7b56 4&#xff1a;memory order \u7684\u7cbe\u786e\u9009\u62e9<\/h4>\n<p>\u56de\u987e\u4e00\u4e0b\u6bcf\u4e2a\u539f\u5b50\u64cd\u4f5c\u7684 memory order&#xff1a;<\/p>\n<table>\n<tbody>\n<tr>\n<p>\u64cd\u4f5c<\/p>\n<p>memory order<\/p>\n<p>\u4e3a\u4ec0\u4e48<\/p>\n<\/tr>\n<tr>\n<td>\n<p>tail_.load()\u00a0\u751f\u4ea7\u8005\u81ea\u5df1\u8bfb tail<\/p>\n<\/td>\n<td>\n<p>relaxed<\/p>\n<\/td>\n<td>\n<p>\u53ea\u6709\u81ea\u5df1\u5199 tail&#xff0c;\u8bfb\u81ea\u5df1\u7684\u503c\u4e0d\u9700\u8981\u540c\u6b65<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>head_.load()\u00a0\u751f\u4ea7\u8005\u8bfb\u6d88\u8d39\u8005\u7684 head<\/p>\n<\/td>\n<td>\n<p>acquire<\/p>\n<\/td>\n<td>\n<p>\u9700\u8981\u770b\u5230\u6d88\u8d39\u8005\u5728\u00a0head_.store(release)\u00a0\u4e4b\u524d\u505a\u7684\u6790\u6784<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>tail_.store()\u00a0\u751f\u4ea7\u8005\u53d1\u5e03 tail<\/p>\n<\/td>\n<td>\n<p>release<\/p>\n<\/td>\n<td>\n<p>\u4fdd\u8bc1 buffer \u7684\u6784\u9020\u5bf9\u6d88\u8d39\u8005\u53ef\u89c1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>head_.load()\u00a0\u6d88\u8d39\u8005\u81ea\u5df1\u8bfb head<\/p>\n<\/td>\n<td>\n<p>relaxed<\/p>\n<\/td>\n<td>\n<p>\u53ea\u6709\u81ea\u5df1\u5199 head<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>tail_.load()\u00a0\u6d88\u8d39\u8005\u8bfb\u751f\u4ea7\u8005\u7684 tail<\/p>\n<\/td>\n<td>\n<p>acquire<\/p>\n<\/td>\n<td>\n<p>\u9700\u8981\u770b\u5230\u751f\u4ea7\u8005\u5728\u00a0tail_.store(release)\u00a0\u4e4b\u524d\u505a\u7684\u6784\u9020<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>head_.store()\u00a0\u6d88\u8d39\u8005\u53d1\u5e03 head<\/p>\n<\/td>\n<td>\n<p>release<\/p>\n<\/td>\n<td>\n<p>\u4fdd\u8bc1 buffer \u7684\u6790\u6784\u5b8c\u6210\u540e\u518d\u8ba9\u751f\u4ea7\u8005\u770b\u5230\u7a7a\u51fa\u7684 slot<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6ca1\u6709\u4efb\u4f55\u5730\u65b9\u9700\u8981\u00a0seq_cst\u3002\u6bcf\u4e00\u4e2a memory order \u90fd\u6709\u7cbe\u786e\u7684\u7406\u7531\u2014\u2014\u4e0d\u662f\u201c\u4e3a\u4e86\u5b89\u5168\u201d\u800c\u5347\u7ea7&#xff0c;\u4e5f\u4e0d\u662f\u201c\u4e3a\u4e86\u6027\u80fd\u201d\u800c\u964d\u7ea7\u3002<\/p>\n<h4>\u8fd9\u4e2a\u5b9e\u73b0\u7684\u5c40\u9650\u6027&#xff08;\u53cd\u8bc1\u81ea\u5df1\u7684\u65b9\u6848&#xff09;<\/h4>\n<p>\u8fd9\u4e2a SPSC ring \u4e0d\u662f\u4e07\u80fd\u7684&#xff1a;<\/p>\n<li>\n<p>\u56fa\u5b9a\u5bb9\u91cf\u3002\u5982\u679c\u6d88\u8d39\u8005\u5904\u7406\u901f\u5ea6\u8ddf\u4e0d\u4e0a\u751f\u4ea7\u8005&#xff0c;push \u8fd4\u56de false&#xff0c;\u751f\u4ea7\u8005\u5fc5\u987b\u81ea\u5df1\u5904\u7406\u80cc\u538b&#xff08;\u4e22\u5f03\u3001\u963b\u585e\u7b49\u5f85\u3001\u6216\u8005\u56de\u9000\u5230\u66f4\u5927\u7684\u961f\u5217&#xff09;\u3002\u52a8\u6001\u6269\u5bb9\u7684\u65e0\u9501\u961f\u5217\u5b58\u5728\u4f46\u590d\u6742\u5f97\u591a&#xff0c;\u4e14\u4f1a\u5931\u53bb cache line padding \u7684\u7cbe\u786e\u63a7\u5236\u3002<\/p>\n<\/li>\n<li>\n<p>\u5bb9\u91cf\u5fc5\u987b\u662f 2 \u7684\u5e42\u3002\u5982\u679c\u4f60\u7684\u6700\u4f73\u5bb9\u91cf\u6070\u597d\u662f 1000&#xff0c;\u4f60\u5f97\u5411\u4e0a\u53d6\u5230 1024&#xff0c;\u6d6a\u8d39 2.4% \u7684\u7a7a\u95f4\u3002\u5bf9\u4e8e\u5927\u578b buffer&#xff08;\u6bd4\u5982\u6bcf\u4e2a slot \u662f 4KB \u7684\u7f51\u7edc buffer&#xff09;&#xff0c;\u6d6a\u8d39\u91cf\u4e0d\u53ef\u5ffd\u7565\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e0d\u652f\u6301\u6279\u91cf\u64cd\u4f5c\u3002\u9ad8\u541e\u5410\u573a\u666f\u4e0b&#xff0c;\u4e00\u6b21 push\/pop \u4e00\u4e2a\u5143\u7d20\u7684\u63a5\u53e3\u4f1a\u56e0\u4e3a function call overhead \u548c\u00a0tail_\/head_\u00a0\u7684\u9891\u7e41\u539f\u5b50\u64cd\u4f5c\u800c\u6210\u4e3a\u74f6\u9888\u3002\u66f4\u597d\u7684\u63a5\u53e3\u662f\u00a0push_batch(T*, size_t)\u00a0\/\u00a0pop_batch(T*, size_t)&#xff0c;\u4e00\u6b21\u66f4\u65b0\u6307\u9488\u3001\u644a\u8584\u539f\u5b50\u64cd\u4f5c\u5f00\u9500\u3002<\/p>\n<\/li>\n<li>\n<p>\u6790\u6784\u7684\u4f4d\u7f6e\u3002\u5f53\u524d\u5b9e\u73b0\u5728\u00a0pop\u00a0\u5185\u90e8\u505a\u00a0buffer_[h &amp; mask_].~T()&#xff0c;\u5982\u679c T \u7684\u6790\u6784\u51fd\u6570\u629b\u5f02\u5e38&#xff08;\u867d\u7136\u4e0d\u5e94\u8be5&#xff09;&#xff0c;head_\u00a0\u5df2\u7ecf\u66f4\u65b0\u4e86\u4f46\u5143\u7d20\u6ca1\u88ab\u6b63\u786e\u6790\u6784\u3002noexcept\u00a0\u7684\u00a0static_assert\u00a0\u53ea\u68c0\u67e5\u4e86\u6784\u9020&#xff0c;\u6790\u6784\u4e5f\u5e94\u8be5\u68c0\u67e5\u3002<\/p>\n<\/li>\n<hr \/>\n<h3>\u7f16\u8bd1\u5668\u4e0e\u786c\u4ef6\u7684\u5408\u8c0b&#xff1a;\u4e3a\u4ec0\u4e48 volatile \u6551\u4e0d\u4e86\u4f60<\/h3>\n<p>\u5199\u5230\u8fd9\u91cc&#xff0c;\u5fc5\u987b\u6b63\u9762\u56de\u5e94\u4e00\u4e2a\u6301\u7eed\u4e86\u4e8c\u5341\u5e74\u7684\u9519\u8bef\u505a\u6cd5&#xff1a;**\u7528\u00a0volatile\u00a0\u505a\u7ebf\u7a0b\u540c\u6b65**\u3002<\/p>\n<p>volatile\u00a0\u7684\u8bed\u4e49\u662f\u201c\u6bcf\u6b21\u8bfb\u5199\u90fd\u771f\u6b63\u53bb\u5185\u5b58\u505a&#xff0c;\u4e0d\u8981\u7f13\u5b58\u5728\u5bc4\u5b58\u5668\u91cc\u201d\u3002\u5b83\u5bf9\u4ed8\u7684\u662f**\u7f16\u8bd1\u5668\u4f18\u5316**\u2014\u2014\u963b\u6b62\u7f16\u8bd1\u5668\u628a\u591a\u6b21\u8bfb\u5408\u5e76\u6210\u4e00\u6b21\u3001\u628a\u6ca1\u88ab\u540e\u7eed\u4ee3\u7801\u5f15\u7528\u7684\u5199\u5220\u9664\u3002\u4f46\u5b83\u5b8c\u5168\u4e0d\u5904\u7406\u4e24\u4e2a\u95ee\u9898&#xff1a;<\/p>\n<li>\n<p>CPU \u6307\u4ee4\u91cd\u6392\u3002volatile\u00a0\u4e0d\u751f\u6210\u4efb\u4f55\u786c\u4ef6\u5c4f\u969c\u6307\u4ee4\u3002ARM CPU \u53ef\u4ee5\u628a\u00a0volatile\u00a0\u5199\u91cd\u6392\u5230\u540e\u7eed\u7684\u00a0volatile\u00a0\u8bfb\u4e4b\u524d\u3002<\/p>\n<\/li>\n<li>\n<p>store buffer \u53ef\u89c1\u6027\u3002CPU \u7684 store buffer \u53ef\u80fd\u5ef6\u8fdf\u00a0volatile\u00a0\u5199\u5bf9\u5176\u4ed6\u6838\u5fc3\u7684\u53ef\u89c1\u6027\u3002<\/p>\n<\/li>\n<p>C&#043;&#043;<\/p>\n<p>\/\/ \u9519\u8bef\u793a\u8303&#xff1a;\u7528 volatile \u505a SPSC \u540c\u6b65<br \/>\nvolatile int flag &#061; 0;<br \/>\nvolatile int payload &#061; 0;<\/p>\n<p>\/\/ \u751f\u4ea7\u8005<br \/>\npayload &#061; 42;<br \/>\nflag &#061; 1;       \/\/ volatile store&#xff0c;\u7f16\u8bd1\u5668\u4e0d\u4f1a\u91cd\u6392\u8fd9\u4e24\u884c<br \/>\n                \/\/ \u4f46 CPU \u53ef\u4ee5&#xff01;ARM \u4e0a flag &#061; 1 \u53ef\u80fd\u5148\u4e8e payload &#061; 42 \u5230\u8fbe\u7f13\u5b58<\/p>\n<p>\/\/ \u6d88\u8d39\u8005<br \/>\nwhile (flag &#061;&#061; 0) {}<br \/>\nint x &#061; payload; \/\/ \u53ef\u80fd\u8bfb\u5230 0&#xff0c;\u4e0d\u662f 42<\/p>\n<p>\u5728 x86 \u4e0a\u8fd9\u6bb5\u4ee3\u7801\u201c\u78b0\u5de7\u6b63\u786e\u201d\u2014\u2014TSO \u4fdd\u8bc1\u4e86 StoreStore \u987a\u5e8f\u3002\u5728 ARM \u4e0a&#xff0c;volatile\u00a0\u5199\u7f16\u8bd1\u6210\u666e\u901a\u00a0STR&#xff0c;CPU \u6709\u6743\u91cd\u6392\u3002<\/p>\n<p>\u66f4\u7cdf\u7cd5\u7684\u662f&#xff0c;volatile\u00a0\u548c\u00a0std::atomic\u00a0\u6df7\u7528\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u65e0\u6cd5\u63a8\u7406\u5185\u5b58\u6a21\u578b&#xff0c;\u4ea7\u751f\u5fae\u5999\u7684 bug\u3002C&#043;&#043; \u6807\u51c6\u660e\u786e\u6307\u51fa&#xff08;[intro.races] \u00a76.9.2.1&#xff09;&#xff1a;volatile\u00a0\u4e0d\u6784\u6210\u7ebf\u7a0b\u95f4\u7684 happens-before \u5173\u7cfb\u3002<\/p>\n<p>**\u7ed3\u8bba&#xff1a;\u5728 C&#043;&#043;11 \u4e4b\u540e&#xff0c;volatile\u00a0\u5728\u591a\u7ebf\u7a0b\u540c\u6b65\u573a\u666f\u4e2d\u6ca1\u6709\u4efb\u4f55\u5408\u6cd5\u7528\u9014\u3002** \u5b83\u552f\u4e00\u7684\u6b63\u5f53\u7528\u9014\u662f\u5185\u5b58\u6620\u5c04 I\/O&#xff08;MMIO&#xff09;&#xff0c;\u5bf9\u4ed8\u7684\u662f\u786c\u4ef6\u5bc4\u5b58\u5668&#xff0c;\u4e0d\u662f\u7ebf\u7a0b\u3002<\/p>\n<hr \/>\n<h3>seq_cst \u7684\u771f\u5b9e\u4ee3\u4ef7&#xff1a;x86 \u4e0e ARM \u7684\u975e\u5bf9\u79f0\u5f71\u54cd<\/h3>\n<p>\u524d\u9762\u8bf4\u4e86\u201c\u5927\u591a\u6570\u65e0\u9501\u961f\u5217\u4e0d\u9700\u8981 seq_cst\u201d&#xff0c;\u4f46\u5230\u5e95\u8d35\u591a\u5c11&#xff1f;<\/p>\n<p>\u5728 x86 \u4e0a&#xff0c;seq_cst store \u9700\u8981\u4e00\u6761\u00a0MFENCE\u00a0\u6216\u8005\u7528\u00a0LOCK XCHG\u00a0\u4ee3\u66ff\u666e\u901a\u00a0MOV\u3002MFENCE\u00a0\u5728 Skylake \u67b6\u6784\u4e0a\u7684\u5ef6\u8fdf\u7ea6 33 \u4e2a\u5468\u671f&#xff08;Intel Optimization Manual&#xff09;&#xff0c;\u800c\u4e14\u5b83\u4f1a flush store buffer\u3001stall \u6d41\u6c34\u7ebf\u3002\u4f46 seq_cst load \u5728 x86 \u4e0a\u662f\u514d\u8d39\u7684\u2014\u2014\u8ddf acquire load \u751f\u6210\u5b8c\u5168\u76f8\u540c\u7684\u00a0MOV\u3002<\/p>\n<p>\u5728 ARM \u4e0a&#xff0c;seq_cst \u7684\u4ee3\u4ef7\u66f4\u4e0d\u5bf9\u79f0\u3002seq_cst store\u00a0\u7f16\u8bd1\u6210\u00a0STLR&#xff08;\u8ddf release \u4e00\u6837&#xff09;&#xff0c;\u4f46\u00a0seq_cst load\u00a0\u53ef\u80fd\u9700\u8981\u5728\u00a0LDAR\u00a0\u4e4b\u540e\u989d\u5916\u63d2\u5165\u4e00\u6761\u00a0DMB ISH&#xff08;Data Memory Barrier, Inner Shareable&#xff09;&#xff0c;\u4ee5\u4fdd\u8bc1\u5168\u5e8f\u3002\u5177\u4f53\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668\u548c\u4e0a\u4e0b\u6587\u2014\u2014GCC 13 \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u4f18\u5316\u6389\u8fd9\u4e2a\u00a0DMB&#xff0c;\u4f46\u4e0d\u4fdd\u8bc1\u3002<\/p>\n<p>\u4e00\u6b21\u00a0DMB ISH\u00a0\u5728 Cortex-A78&#xff08;Graviton3 \u4f7f\u7528\u7684\u6838\u5fc3&#xff09;\u4e0a\u5927\u7ea6 30-50 \u4e2a\u5468\u671f&#xff0c;\u800c\u4e14\u5b83\u4f1a stall \u6574\u4e2a\u6d41\u6c34\u7ebf\u76f4\u5230\u4e4b\u524d\u7684\u6240\u6709\u5185\u5b58\u64cd\u4f5c\u90fd\u5b8c\u6210\u3002\u5728\u9ad8\u9891\u5faa\u73af\u4e2d&#xff08;\u6bd4\u5982\u65e0\u9501\u961f\u5217\u7684 CAS \u81ea\u65cb&#xff09;&#xff0c;\u8fd9\u4e2a\u4ee3\u4ef7\u4f1a\u53e0\u52a0\u3002<\/p>\n<p>\u56de\u5230\u90a3\u4e2a benchmark&#xff1a;SPSC ring buffer&#xff0c;\u5bb9\u91cf 65536&#xff0c;GCC 13 -O2&#xff0c;1 \u4ebf\u6b21\u64cd\u4f5c\u3002<\/p>\n<p>text<\/p>\n<p>                     x86-64 (Xeon Gold 6348)    AArch64 (Graviton3)<br \/>\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500    \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500     \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\n\u5168\u90e8 seq_cst         ~300M ops\/sec               ~185M ops\/sec<br \/>\n\u6b63\u786e acq\/rel         ~320M ops\/sec               ~225M ops\/sec<br \/>\n\u5168\u90e8 relaxed&#xff08;BUG&#xff09;   ~325M ops\/sec               ~260M ops\/sec&#xff08;\u4f46\u6570\u636e\u9519&#xff09;<\/p>\n<p>x86 \u4e0a\u4ece seq_cst \u6362\u5230 acquire\/release \u53ea\u5feb\u4e86\u7ea6 6%&#xff08;\u56e0\u4e3a\u4e3b\u8981\u5dee\u5f02\u53ea\u5728 store \u7aef\u7684\u00a0MFENCE&#xff09;\u3002ARM \u4e0a\u5feb\u4e86\u7ea6 22%\u2014\u2014\u56e0\u4e3a ARM \u7684\u6bcf\u4e00\u7ea7 memory order \u90fd\u5bf9\u5e94\u4e0d\u540c\u7684\u6307\u4ee4&#xff0c;acquire\/release \u76f8\u5bf9 seq_cst \u5c11\u4e86\u989d\u5916\u7684 barrier\u3002<\/p>\n<p>\u800c\u4ece acquire\/release \u518d\u964d\u5230 relaxed&#xff0c;x86 \u51e0\u4e4e\u65e0\u53d8\u5316&#xff08;\u672c\u6765\u5c31\u662f\u540c\u4e00\u6761\u00a0MOV&#xff09;&#xff0c;ARM \u4e0a\u53c8\u5feb\u4e86 16%\u2014\u2014\u4f46\u6570\u636e\u662f\u9519\u7684\u3002**\u5728 ARM \u4e0a&#xff0c;relaxed \u7684\u201c\u6027\u80fd\u63d0\u5347\u201d\u662f\u7528\u6b63\u786e\u6027\u6362\u6765\u7684\u3002**<\/p>\n<p>\u8fd9\u7ec4\u6570\u5b57\u8bf4\u660e\u4e86\u4e00\u4e2a\u91cd\u8981\u7ed3\u8bba&#xff1a;memory order \u7684\u9009\u62e9\u5bf9\u6027\u80fd\u7684\u5f71\u54cd\u662f\u5e73\u53f0\u975e\u5bf9\u79f0\u7684\u3002x86 \u4e0a acquire\/release \u51e0\u4e4e\u514d\u8d39&#xff0c;\u6ca1\u6709\u7406\u7531\u7528 relaxed&#xff1b;ARM \u4e0a acquire\/release \u6709\u7ea6 15-20% \u7684\u5f00\u9500\u4f46\u8fd9\u662f\u6b63\u786e\u6027\u7684\u4ee3\u4ef7&#xff0c;\u4e0d\u80fd\u7701\u3002<\/p>\n<hr \/>\n<h3>\u5b9e\u6d4b\u9a8c\u8bc1&#xff1a;\u7528 ThreadSanitizer \u548c LKMM Litmus \u6d4b\u8bd5\u4f60\u7684\u5b9e\u73b0<\/h3>\n<p>\u8bb2\u4e86\u8fd9\u4e48\u591a\u6b63\u786e\u6027\u548c\u6027\u80fd\u7684\u53d6\u820d&#xff0c;\u8fd8\u5269\u4e00\u4e2a\u5b9e\u9645\u95ee\u9898\u8981\u56de\u7b54&#xff1a;\u600e\u4e48\u9a8c\u8bc1\u4f60\u7684\u65e0\u9501\u961f\u5217\u5b9e\u73b0\u662f\u6b63\u786e\u7684&#xff1f;\u201c\u5728 x86 \u4e0a\u8dd1\u901a\u6d4b\u8bd5\u201d\u663e\u7136\u4e0d\u591f\u3002<\/p>\n<h4>ThreadSanitizer&#xff08;TSan&#xff09;<\/h4>\n<p>GCC \u548c Clang \u90fd\u652f\u6301\u00a0-fsanitize&#061;thread\u3002TSan \u5728\u8fd0\u884c\u65f6\u62e6\u622a\u6240\u6709\u5185\u5b58\u8bbf\u95ee\u548c\u540c\u6b65\u64cd\u4f5c&#xff0c;\u68c0\u6d4b data race\u3002\u5b83\u80fd\u68c0\u6d4b\u5230\u5927\u90e8\u5206\u201c\u5b8c\u5168\u6f0f\u6389\u540c\u6b65\u201d\u7684\u60c5\u51b5&#xff1a;<\/p>\n<p>Bash<\/p>\n<p># GCC 13, -O1 -g -fsanitize&#061;thread<br \/>\ng&#043;&#043; -O1 -g -fsanitize&#061;thread -std&#061;c&#043;&#043;20 spsc_test.cpp -o spsc_test<br \/>\n.\/spsc_test<\/p>\n<p>\u4f46 TSan \u6709\u4e00\u4e2a\u91cd\u8981\u7684\u5c40\u9650&#xff1a;\u5b83\u4e0d\u68c0\u6d4b memory order \u9009\u62e9\u9519\u8bef\u3002\u00a0\u5982\u679c\u4f60\u7528\u4e86\u00a0std::atomic\u00a0\u5e76\u4e14\u9009\u62e9\u4e86\u00a0relaxed&#xff0c;TSan \u4f1a\u8ba4\u4e3a\u4f60\u662f\u6709\u610f\u4e3a\u4e4b\u2014\u2014\u4f60\u201c\u9009\u62e9\u4e86\u5f31\u540c\u6b65\u8bed\u4e49\u201d\u3002TSan \u4e0d\u4f1a\u62a5\u544a\u201c\u4f60\u8fd9\u91cc\u5e94\u8be5\u7528 release \u800c\u4e0d\u662f relaxed\u201d\u3002<\/p>\n<p>\u6240\u4ee5 TSan \u80fd\u6293\u5230\u201c\u5fd8\u4e86\u7528 atomic\u201d\u7684 bug&#xff0c;\u6293\u4e0d\u5230\u201c\u7528\u4e86 atomic \u4f46 memory order \u9009\u9519\u201d\u7684 bug\u3002\u540e\u8005\u6b63\u662f x86 \u5230 ARM \u79fb\u690d\u65f6\u6700\u5e38\u9047\u5230\u7684\u95ee\u9898\u3002<\/p>\n<h4>C\/C&#043;&#043; Memory Model Litmus Tests<\/h4>\n<p>\u66f4\u7cbe\u786e\u7684\u5de5\u5177\u662f\u00a0herd7&#xff08;\u6765\u81ea INRIA \u7684 diy7 \u5de5\u5177\u5957\u4ef6&#xff09;&#xff0c;\u5b83\u53ef\u4ee5\u5728\u62bd\u8c61\u7684 C11 \u5185\u5b58\u6a21\u578b\u4e0a\u7a77\u4e3e\u6240\u6709\u5408\u6cd5\u7684\u6267\u884c\u5e8f\u5217&#xff0c;\u5224\u65ad\u4e00\u6bb5\u4ee3\u7801\u662f\u5426\u5b58\u5728 data race \u6216\u8fdd\u53cd\u7ea6\u675f\u7684\u6267\u884c\u8def\u5f84\u3002<\/p>\n<p>\u5199\u4e00\u4e2a litmus test \u6765\u9a8c\u8bc1\u6211\u4eec\u7684 SPSC ring buffer \u7684\u5173\u952e\u4e0d\u53d8\u5f0f&#xff1a;<\/p>\n<p>text<\/p>\n<p>C spsc-release-acquire<\/p>\n<p>{<br \/>\n    [x] &#061; 0;  (* payload *)<br \/>\n    [flag] &#061; 0; (* tail *)<br \/>\n}<\/p>\n<p>P0 (int *x, atomic_int *flag) {<br \/>\n    *x &#061; 42;<br \/>\n    atomic_store_explicit(flag, 1, memory_order_release);<br \/>\n}<\/p>\n<p>P1 (int *x, atomic_int *flag) {<br \/>\n    int r1 &#061; atomic_load_explicit(flag, memory_order_acquire);<br \/>\n    if (r1 &#061;&#061; 1) {<br \/>\n        int r2 &#061; *x;<br \/>\n    }<br \/>\n}<\/p>\n<p>exists (1:r1&#061;1 \/\\\\ 1:r2&#061;0)<br \/>\n(* \u95ee&#xff1a;\u6d88\u8d39\u8005\u662f\u5426\u53ef\u80fd\u770b\u5230 flag&#061;1 \u4f46 payload \u8fd8\u662f 0&#xff1f; *)<br \/>\n(* \u7b54&#xff1a;No\u3002acquire\/release \u914d\u5bf9\u4fdd\u8bc1\u4e86 happens-before\u3002 *)<\/p>\n<p>\u7528 herd7 \u8fd0\u884c&#xff1a;<\/p>\n<p>Bash<\/p>\n<p>herd7 spsc-release-acquire.litmus<\/p>\n<p>\u5982\u679c\u628a\u00a0memory_order_release\u00a0\u6539\u6210\u00a0memory_order_relaxed&#xff0c;herd7 \u4f1a\u62a5\u544a\u00a0exists\u00a0\u6761\u4ef6\u53ef\u6ee1\u8db3\u2014\u2014\u5373\u6d88\u8d39\u8005\u53ef\u80fd\u770b\u5230 flag&#061;1 \u4f46 payload&#061;0\u3002\u8fd9\u76f4\u63a5\u8bc1\u660e\u4e86 relaxed \u5728\u5f31\u5185\u5b58\u6a21\u578b\u4e0a\u7684\u4e0d\u5b89\u5168\u6027\u3002<\/p>\n<h4>\u5728 ARM \u786c\u4ef6\u4e0a\u5b9e\u9645\u89e6\u53d1<\/h4>\n<p>\u6700\u76f4\u63a5\u7684\u65b9\u6cd5&#xff1a;\u5728\u771f\u6b63\u7684 ARM \u673a\u5668\u4e0a\u8dd1\u538b\u529b\u6d4b\u8bd5\u3002AWS Graviton&#xff08;\u57fa\u4e8e ARM Neoverse&#xff09;\u5b9e\u4f8b\u662f\u6700\u65b9\u4fbf\u7684\u9009\u62e9&#xff1a;<\/p>\n<p>Bash<\/p>\n<p># \u5728 Graviton3 \u5b9e\u4f8b\u4e0a\u7f16\u8bd1\u8fd0\u884c<br \/>\naarch64-linux-gnu-g&#043;&#043; -O2 -std&#061;c&#043;&#043;20 -march&#061;armv8.2-a spsc_test.cpp -o spsc_test<br \/>\n# \u7ed1\u5b9a\u4e24\u4e2a\u6838\u5fc3&#xff0c;\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u5206\u522b\u5728\u4e0d\u540c\u6838\u5fc3<br \/>\ntaskset -c 0 .\/spsc_producer &amp;<br \/>\ntaskset -c 1 .\/spsc_consumer<\/p>\n<p>\u5982\u679c\u4f60\u7684\u5b9e\u73b0\u6709 memory order \u9519\u8bef&#xff0c;\u5728 Graviton \u4e0a\u8dd1 10 \u4ebf\u6b21\u64cd\u4f5c\u901a\u5e38\u80fd\u5728\u51e0\u5206\u949f\u5185\u89e6\u53d1\u3002\u6211\u7684\u7ecf\u9a8c\u662f&#xff1a;\u9519\u8bef\u7684 relaxed store \u5728 Graviton3 \u4e0a\u5927\u7ea6\u6bcf\u767e\u4e07\u6b21\u64cd\u4f5c\u51fa\u4e00\u6b21\u53ef\u89c2\u6d4b\u7684\u9519\u8bef\u2014\u2014\u9891\u7387\u4e0d\u7b97\u9ad8\u4f46\u7edd\u4e0d\u662f\u7406\u8bba\u4e0a\u7684\u3002<\/p>\n<p>\u800c\u5728 x86 \u4e0a\u8dd1\u540c\u6837\u7684\u6d4b\u8bd5\u2014\u2014\u5373\u4f7f\u4f60\u628a\u6240\u6709 memory order \u90fd\u6539\u6210 relaxed\u2014\u2014\u4e00\u5343\u4ebf\u6b21\u4e5f\u672a\u5fc5\u51fa\u4e00\u6b21\u3002\u56e0\u4e3a TSO \u5728\u66ff\u4f60\u515c\u5e95\u3002<\/p>\n<hr \/>\n<h3>C&#043;&#043;20\/23\/26 \u7684\u6f14\u8fdb&#xff1a;\u6807\u51c6\u5728\u8ffd\u8d76\u5de5\u7a0b\u5b9e\u8df5<\/h3>\n<p>\u518d\u770b\u4e00\u4e0b\u6807\u51c6\u6f14\u8fdb\u3002C&#043;&#043;11 \u5f15\u5165\u00a0&lt;atomic&gt;\u00a0\u81f3\u4eca\u5df2\u7ecf\u5341\u591a\u5e74&#xff0c;\u540e\u7eed\u6807\u51c6\u4e00\u76f4\u5728\u8865\u9f50\u5e76\u53d1\u5de5\u5177\u94fe&#xff1a;<\/p>\n<p>C&#043;&#043;20&#xff1a;<\/p>\n<ul>\n<li>\n<p>std::atomic&lt;T&gt;::wait()\u00a0\/\u00a0notify_one()\u00a0\/\u00a0notify_all()&#xff1a;\u539f\u5b50\u53d8\u91cf\u76f4\u63a5\u652f\u6301\u7b49\u5f85\/\u901a\u77e5&#xff0c;\u4e0d\u518d\u9700\u8981\u989d\u5916\u7684\u6761\u4ef6\u53d8\u91cf\u3002\u5bf9 SPSC ring \u7684\u80cc\u538b\u7b56\u7565\u6709\u610f\u4e49\u2014\u2014\u6ee1\u65f6\u53ef\u4ee5\u00a0tail_.wait(old_tail)\u00a0\u800c\u4e0d\u662f\u5fd9\u7b49\u3002<\/p>\n<\/li>\n<li>\n<p>std::atomic_ref&lt;T&gt;&#xff1a;\u5bf9\u975e\u539f\u5b50\u5bf9\u8c61\u505a\u539f\u5b50\u64cd\u4f5c\u3002\u5bf9 buffer pool \u4e2d\u201c\u6709\u65f6\u9700\u8981\u539f\u5b50\u8bbf\u95ee\u3001\u6709\u65f6\u4e0d\u9700\u8981\u201d\u7684\u573a\u666f\u6709\u7528\u3002<\/p>\n<\/li>\n<\/ul>\n<p>C&#043;&#043;23&#xff1a;<\/p>\n<ul>\n<li>\n<p>std::atomic&lt;T&gt;::operator&#061;\u00a0\u7684 value \u8fd4\u56de&#xff08;P0558&#xff09;&#xff1a;\u6d88\u9664\u4e86\u4e00\u4e2a\u957f\u671f\u7684 API \u4e0d\u4e00\u81f4\u3002<\/p>\n<\/li>\n<li>\n<p>\u66f4\u597d\u7684\u00a0constexpr\u00a0\u652f\u6301&#xff1a;\u539f\u5b50\u64cd\u4f5c\u53ef\u4ee5\u5728\u7f16\u8bd1\u671f\u6c42\u503c&#xff08;\u6709\u9650\u573a\u666f&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<p>C&#043;&#043;26&#xff08;\u5df2\u8fdb\u5165 Working Draft&#xff09;&#xff1a;<\/p>\n<ul>\n<li>\n<p>&lt;hazard_pointer&gt;&#xff08;P2530&#xff09;&#xff1a;\u6807\u51c6\u5316 hazard pointer&#xff0c;\u4e0d\u518d\u9700\u8981\u81ea\u5df1\u5b9e\u73b0\u6216\u4f9d\u8d56\u7b2c\u4e09\u65b9\u5e93\u3002<\/p>\n<\/li>\n<li>\n<p>&lt;rcu&gt;&#xff08;P2545&#xff09;&#xff1a;Read-Copy-Update&#xff0c;\u53e6\u4e00\u79cd\u5b89\u5168\u56de\u6536\u673a\u5236\u3002<\/p>\n<\/li>\n<li>\n<p>\u8fd9\u4e24\u4e2a\u6807\u51c6\u5316\u7684\u610f\u4e49\u4e0d\u53ea\u662f\u201c\u6709\u4e86\u5b98\u65b9\u5b9e\u73b0\u201d\u2014\u2014\u66f4\u91cd\u8981\u7684\u662f\u7f16\u8bd1\u5668\u53ef\u4ee5\u9488\u5bf9\u6807\u51c6 API \u505a\u4f18\u5316&#xff0c;\u6bd4\u5982\u628a hazard pointer \u7684 protect \u64cd\u4f5c\u8ddf\u540e\u7eed\u7684 load \u5408\u5e76\u3001\u6d88\u9664\u4e0d\u5fc5\u8981\u7684 fence\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u5bf9\u4e8e\u4eca\u5929\u5199\u65e0\u9501\u961f\u5217\u7684\u5de5\u7a0b\u5e08\u6765\u8bf4&#xff0c;C26 \u7684\u00a0&lt;hazard_pointer&gt;\u00a0\u662f\u503c\u5f97\u5173\u6ce8\u7684\u2014\u2014\u5b83\u610f\u5473\u7740\u4f60\u4e0d\u518d\u9700\u8981\u5728 tagged pointer \u548c\u81ea\u5df1\u624b\u6413\u7684 EBR \u4e4b\u95f4\u505a\u9009\u62e9&#xff0c;\u6807\u51c6\u5e93\u4f1a\u7ed9\u4f60\u4e00\u4e2a\u7ecf\u8fc7\u5145\u5206\u9a8c\u8bc1\u3001\u7f16\u8bd1\u5668\u53cb\u597d\u7684\u5b9e\u73b0\u3002\u4f46\u5728 C26 \u666e\u53ca\u4e4b\u524d&#xff08;\u53ef\u80fd\u8981\u5230 2028 \u5e74\u5de6\u53f3\u7f16\u8bd1\u5668\u624d\u4f1a\u5b8c\u6574\u652f\u6301&#xff09;&#xff0c;Folly \u7684\u00a0folly::hazard_pointer\u00a0\u662f\u6700\u6210\u719f\u7684\u751f\u4ea7\u7ea7\u9009\u62e9\u3002<\/p>\n<hr \/>\n<h3>\u5173\u4e8e\u8de8\u67b6\u6784\u79fb\u690d\u7684\u65b9\u6cd5\u8bba&#xff1a;\u4e0d\u8981\u5728\u4f60\u624b\u5934\u7684\u673a\u5668\u4e0a\u8dd1\u8fc7\u5c31\u7b97\u6570<\/h3>\n<p>\u603b\u7ed3\u8fd9\u7bc7\u6587\u7ae0\u7684\u6838\u5fc3\u5224\u65ad&#xff1a;<\/p>\n<p>\u5927\u91cf\u65e0\u9501\u961f\u5217\u5b9e\u73b0\u7684\u201c\u6b63\u786e\u6027\u201d\u662f x86-TSO \u786c\u4ef6\u6a21\u578b\u7684\u8d60\u54c1&#xff0c;\u4e0d\u662f\u5f00\u53d1\u8005\u7684\u8bbe\u8ba1\u3002\u00a0\u8fc1\u79fb\u5230 ARM \u662f\u4e00\u6b21\u5f3a\u5236\u6027\u7684\u6b63\u786e\u6027\u5ba1\u8ba1\u2014\u2014\u4f60\u4ee5\u4e3a\u81ea\u5df1\u5728\u7528 acquire\/release \u505a\u540c\u6b65&#xff0c;\u5b9e\u9645\u4e0a\u53ea\u662f\u5728\u7528\u201c\u6070\u597d\u4e0d\u91cd\u6392\u7684 CPU \u4e0a\u7684\u6070\u597d\u4e0d\u91cd\u6392\u7684\u6307\u4ee4\u201d\u3002<\/p>\n<p>\u6b63\u786e\u7684\u5de5\u4f5c\u65b9\u5f0f\u662f\u6309\u6700\u5f31\u7684\u76ee\u6807\u67b6\u6784\u8bbe\u8ba1\u540c\u6b65\u903b\u8f91&#xff0c;\u7136\u540e\u8ba9\u7f16\u8bd1\u5668\u5728\u5f3a\u67b6\u6784\u4e0a\u81ea\u52a8\u4f18\u5316\u6389\u4e0d\u5fc5\u8981\u7684\u5c4f\u969c\u3002\u8fd9\u6070\u597d\u662f C&#043;&#043; memory model \u7684\u8bbe\u8ba1\u610f\u56fe\u2014\u2014\u4f60\u58f0\u660e\u8bed\u4e49&#xff08;acquire\/release&#xff09;&#xff0c;\u7f16\u8bd1\u5668\u6839\u636e\u76ee\u6807\u67b6\u6784\u751f\u6210\u6700\u9ad8\u6548\u7684\u6307\u4ee4\u3002\u5728 x86 \u4e0a\u5b83\u514d\u8d39\u7ed9\u4f60\u4e00\u4e2a MOV&#xff0c;\u5728 ARM \u4e0a\u5b83\u7ed9\u4f60\u4e00\u6761 LDAR\/STLR\u3002\u4e24\u8005\u7684\u6b63\u786e\u6027\u90fd\u6709\u4fdd\u8bc1&#xff0c;\u4f46\u4f60\u5fc5\u987b\u5728\u6e90\u7801\u5c42\u9762\u8bf4\u6e05\u695a\u4f60\u8981\u4ec0\u4e48\u8bed\u4e49\u3002<\/p>\n<p>\u540c\u6837\u7684\u65b9\u6cd5\u8bba\u9002\u7528\u4e8e\u961f\u5217\u6a21\u578b\u7684\u9009\u62e9\u3002MPMC \u662f\u4e00\u4e2a\u201c\u4e07\u80fd\u201d\u63a5\u53e3\u2014\u2014\u8c01\u90fd\u80fd push&#xff0c;\u8c01\u90fd\u80fd pop\u2014\u2014\u4f46\u8fd9\u4e2a\u4e07\u80fd\u6027\u7684\u4ee3\u4ef7\u662f\u6240\u6709\u53c2\u4e0e\u8005\u90fd\u5728\u7ade\u4e89\u3002\u5982\u679c\u4f60\u7684\u67b6\u6784\u80fd\u5206\u89e3\u6210 SPSC \u6216 MPSC \u7684\u7ec4\u5408&#xff0c;\u4f60\u5e94\u8be5\u8fd9\u4e48\u505a\u3002\u00a0\u201c\u4e00\u4e2a MPMC \u961f\u5217\u8fde\u63a5\u6240\u6709\u7ebf\u7a0b\u201d\u662f\u6700\u5077\u61d2\u7684\u8bbe\u8ba1&#xff0c;\u4e5f\u662f\u6700\u6162\u7684\u2014\u2014\u56e0\u4e3a\u5b83\u8ba9 N \u4e2a\u7ebf\u7a0b\u7ade\u4e89\u540c\u4e00\u6761 cache line \u4e0a\u7684\u540c\u4e00\u4e2a CAS\u3002<\/p>\n<p>\u6211\u503e\u5411\u4e8e\u8ba4\u4e3a&#xff0c;\u4e00\u4e2a\u7cfb\u7edf\u4e2d\u65e0\u9501\u961f\u5217\u7684\u79cd\u7c7b\u548c\u6570\u91cf\u5e94\u8be5\u88ab\u89c6\u4e3a\u67b6\u6784\u51b3\u7b56&#xff0c;\u800c\u4e0d\u662f\u6027\u80fd\u4f18\u5316\u3002\u9009 SPSC\/MPSC\/MPMC \u4e0d\u662f\u5728\u8c03\u53c2\u6570&#xff0c;\u662f\u5728\u5b9a\u4e49\u7ebf\u7a0b\u95f4\u7684\u901a\u4fe1\u62d3\u6251\u3002\u60f3\u6e05\u695a\u8c01\u8ddf\u8c01\u8bf4\u8bdd\u3001\u5355\u5411\u8fd8\u662f\u53cc\u5411\u3001\u6709\u6ca1\u6709\u80cc\u538b\u2014\u2014\u961f\u5217\u7684\u7c7b\u578b\u5c31\u81ea\u7136\u51fa\u6765\u4e86\u3002<\/p>\n<p>\u8fd9\u4e2a\u95ee\u9898\u5728\u5927\u6a21\u578b\u63a8\u7406\u5f15\u64ce\u4e2d\u540c\u6837\u5b58\u5728\u3002vLLM \u7684 scheduler \u7ebf\u7a0b\u628a\u8bf7\u6c42\u5206\u53d1\u7ed9 worker&#xff0c;worker \u5904\u7406\u5b8c\u628a\u7ed3\u679c\u8fd4\u56de\u2014\u2014\u8fd9\u662f\u4e00\u4e2a\u5178\u578b\u7684\u201c\u4e00\u5bf9\u591a\u5206\u53d1 &#043; \u591a\u5bf9\u4e00\u6536\u96c6\u201d\u7684\u62d3\u6251\u3002\u7528\u4e00\u4e2a MPMC \u961f\u5217\u505a\u5206\u53d1\u5c31\u662f\u5728\u5f3a\u8feb N \u4e2a worker \u7ade\u4e89\u51fa\u961f&#xff1b;\u7528 N \u4e2a SPSC \u961f\u5217&#xff0c;scheduler \u76f4\u63a5\u5f80\u5bf9\u5e94 worker \u7684\u961f\u5217\u91cc push&#xff0c;\u6bcf\u4e2a worker \u72ec\u5360\u6d88\u8d39\u81ea\u5df1\u7684\u961f\u5217\u2014\u2014CAS \u7ade\u4e89\u964d\u5230\u96f6&#xff0c;cache line bouncing \u964d\u5230\u6700\u4f4e\u3002\u6536\u96c6\u7aef\u53cd\u8fc7\u6765&#xff1a;N \u4e2a worker push \u5230\u4e00\u4e2a MPSC \u961f\u5217&#xff0c;scheduler \u72ec\u5360\u6d88\u8d39\u3002<\/p>\n<p>\u8fd9\u4e0d\u662f\u4ec0\u4e48\u9ad8\u6df1\u7684\u67b6\u6784\u6a21\u5f0f\u3002\u4f46\u6211\u89c1\u8fc7\u592a\u591a\u7cfb\u7edf\u2014\u2014\u5305\u62ec\u4e00\u4e9b\u77e5\u540d\u7684\u5f00\u6e90\u9879\u76ee\u2014\u2014\u7528\u4e00\u4e2a MPMC \u961f\u5217\u89e3\u51b3\u4e00\u5207&#xff0c;\u7136\u540e\u82b1\u51e0\u4e2a\u6708\u5728 CAS \u91cd\u8bd5\u7684 backoff \u7b56\u7565\u4e0a\u8c03\u53c2\u3002\u9000\u4e00\u6b65\u9009\u5bf9\u6a21\u578b&#xff0c;\u6bd4\u5728\u9519\u8bef\u7684\u6a21\u578b\u4e0a\u505a\u5fae\u4f18\u5316\u6709\u6548\u5f97\u591a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u91cf GitHub \u4e0a\u7684\u65e0\u9501\u961f\u5217\u5b9e\u73b0\u4f9d\u8d56 x86-TSO \u7684\u5f3a\u5185\u5b58\u6a21\u578b\u201c\u78b0\u5de7\u6b63\u786e\u201d&#xff0c;\u8fc1\u79fb\u5230 ARM&#xff08;\u5305\u62ec Apple Silicon\u3001AWS Graviton&#xff09;\u540e\u7acb\u523b\u66b4\u9732\u7f3a\u5931\u7684 acquire\/release \u5c4f\u969c&#xff0c;\u8868\u73b0\u4e3a\u95f4\u6b47\u6027\u8bfb\u5230\u810f\u6570\u636e\u3002\u672c\u6587\u4ece\u4e00\u6bb5 GCC 13 \u5728 x86-64 \u4e0e AArch64 \u4e0a\u7684\u5bf9\u7167\u53cd\u6c47\u7f16\u51fa\u53d1&#xff0c;\u62c6\u6e05 memory_order_relaxed\/acquire\/release\/seq<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[751,55,2962,3419],"topic":[],"class_list":["post-86344","post","type-post","status-publish","format-standard","hentry","category-server","tag-arm","tag-c","tag-c20","tag-c23"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4ece GCC 13 \u53cd\u6c47\u7f16\u770b\u5185\u5b58\u4e00\u81f4\u6027\uff1a\u4e3a\u4ec0\u4e48\u666e\u901a\u7684 MOV \u5728 ARM \u4e0a\u5fc5\u987b\u53d8\u6210 LDAR\/STLR\uff1f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/86344.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4ece GCC 13 \u53cd\u6c47\u7f16\u770b\u5185\u5b58\u4e00\u81f4\u6027\uff1a\u4e3a\u4ec0\u4e48\u666e\u901a\u7684 MOV \u5728 ARM \u4e0a\u5fc5\u987b\u53d8\u6210 LDAR\/STLR\uff1f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5927\u91cf GitHub \u4e0a\u7684\u65e0\u9501\u961f\u5217\u5b9e\u73b0\u4f9d\u8d56 x86-TSO \u7684\u5f3a\u5185\u5b58\u6a21\u578b\u201c\u78b0\u5de7\u6b63\u786e\u201d&#xff0c;\u8fc1\u79fb\u5230 ARM&#xff08;\u5305\u62ec Apple Silicon\u3001AWS Graviton&#xff09;\u540e\u7acb\u523b\u66b4\u9732\u7f3a\u5931\u7684 acquire\/release \u5c4f\u969c&#xff0c;\u8868\u73b0\u4e3a\u95f4\u6b47\u6027\u8bfb\u5230\u810f\u6570\u636e\u3002\u672c\u6587\u4ece\u4e00\u6bb5 GCC 13 \u5728 x86-64 \u4e0e AArch64 \u4e0a\u7684\u5bf9\u7167\u53cd\u6c47\u7f16\u51fa\u53d1&#xff0c;\u62c6\u6e05 memory_order_relaxed\/acquire\/release\/seq\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/86344.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-28T05:32:36+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"21 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/86344.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/86344.html\",\"name\":\"\u4ece GCC 13 \u53cd\u6c47\u7f16\u770b\u5185\u5b58\u4e00\u81f4\u6027\uff1a\u4e3a\u4ec0\u4e48\u666e\u901a\u7684 MOV \u5728 ARM \u4e0a\u5fc5\u987b\u53d8\u6210 LDAR\/STLR\uff1f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-28T05:32:36+00:00\",\"dateModified\":\"2026-07-28T05:32:36+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/86344.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/86344.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/86344.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4ece GCC 13 \u53cd\u6c47\u7f16\u770b\u5185\u5b58\u4e00\u81f4\u6027\uff1a\u4e3a\u4ec0\u4e48\u666e\u901a\u7684 MOV \u5728 ARM \u4e0a\u5fc5\u987b\u53d8\u6210 LDAR\/STLR\uff1f\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u4ece GCC 13 \u53cd\u6c47\u7f16\u770b\u5185\u5b58\u4e00\u81f4\u6027\uff1a\u4e3a\u4ec0\u4e48\u666e\u901a\u7684 MOV \u5728 ARM \u4e0a\u5fc5\u987b\u53d8\u6210 LDAR\/STLR\uff1f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/86344.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4ece GCC 13 \u53cd\u6c47\u7f16\u770b\u5185\u5b58\u4e00\u81f4\u6027\uff1a\u4e3a\u4ec0\u4e48\u666e\u901a\u7684 MOV \u5728 ARM \u4e0a\u5fc5\u987b\u53d8\u6210 LDAR\/STLR\uff1f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5927\u91cf GitHub \u4e0a\u7684\u65e0\u9501\u961f\u5217\u5b9e\u73b0\u4f9d\u8d56 x86-TSO \u7684\u5f3a\u5185\u5b58\u6a21\u578b\u201c\u78b0\u5de7\u6b63\u786e\u201d&#xff0c;\u8fc1\u79fb\u5230 ARM&#xff08;\u5305\u62ec Apple Silicon\u3001AWS Graviton&#xff09;\u540e\u7acb\u523b\u66b4\u9732\u7f3a\u5931\u7684 acquire\/release \u5c4f\u969c&#xff0c;\u8868\u73b0\u4e3a\u95f4\u6b47\u6027\u8bfb\u5230\u810f\u6570\u636e\u3002\u672c\u6587\u4ece\u4e00\u6bb5 GCC 13 \u5728 x86-64 \u4e0e AArch64 \u4e0a\u7684\u5bf9\u7167\u53cd\u6c47\u7f16\u51fa\u53d1&#xff0c;\u62c6\u6e05 memory_order_relaxed\/acquire\/release\/seq","og_url":"https:\/\/www.wsisp.com\/helps\/86344.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-28T05:32:36+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"21 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/86344.html","url":"https:\/\/www.wsisp.com\/helps\/86344.html","name":"\u4ece GCC 13 \u53cd\u6c47\u7f16\u770b\u5185\u5b58\u4e00\u81f4\u6027\uff1a\u4e3a\u4ec0\u4e48\u666e\u901a\u7684 MOV \u5728 ARM \u4e0a\u5fc5\u987b\u53d8\u6210 LDAR\/STLR\uff1f - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-28T05:32:36+00:00","dateModified":"2026-07-28T05:32:36+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/86344.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/86344.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/86344.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4ece GCC 13 \u53cd\u6c47\u7f16\u770b\u5185\u5b58\u4e00\u81f4\u6027\uff1a\u4e3a\u4ec0\u4e48\u666e\u901a\u7684 MOV \u5728 ARM \u4e0a\u5fc5\u987b\u53d8\u6210 LDAR\/STLR\uff1f"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/86344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=86344"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/86344\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=86344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=86344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=86344"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=86344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}