{"id":78563,"date":"2026-02-27T22:29:24","date_gmt":"2026-02-27T14:29:24","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/78563.html"},"modified":"2026-02-27T22:29:24","modified_gmt":"2026-02-27T14:29:24","slug":"windows%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0-20%ef%bc%88%e7%ba%bf%e7%a8%8b%e5%90%8c%e6%ad%a5-%e4%b8%b4%e7%95%8c%e5%8c%bacritical-section%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/78563.html","title":{"rendered":"Windows\u5b66\u4e60\u7b14\u8bb0-20\uff08\u7ebf\u7a0b\u540c\u6b65-\u4e34\u754c\u533aCritical Section\uff09"},"content":{"rendered":"<h3>1. \u4e34\u754c\u533a\u6982\u8ff0<\/h3>\n<p>\u4e34\u754c\u533a&#xff08;Critical Section&#xff09;\u662f Windows \u63d0\u4f9b\u7684\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\u540c\u6b65\u5bf9\u8c61&#xff0c;\u7528\u4e8e\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\u5728\u540c\u4e00\u8fdb\u7a0b\u5185\u7684\u591a\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u4e92\u65a5\u8bbf\u95ee\u3002\u5b83\u786e\u4fdd\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8fdb\u5165\u4e34\u754c\u533a\u4ee3\u7801\u6bb5&#xff0c;\u4ece\u800c\u907f\u514d\u6570\u636e\u7ade\u4e89\u548c\u4e0d\u4e00\u81f4\u6027\u3002<\/p>\n<h4>\u6838\u5fc3\u7279\u70b9<\/h4>\n<ul>\n<li>\n<p>\u7528\u6237\u6001\u540c\u6b65&#xff1a;\u4e34\u754c\u533a\u5bf9\u8c61\u5728\u7528\u6237\u6001\u5b9e\u73b0\u540c\u6b65&#xff0c;\u9664\u975e\u53d1\u751f\u7ade\u4e89&#xff08;\u5373\u591a\u4e2a\u7ebf\u7a0b\u5c1d\u8bd5\u540c\u65f6\u8fdb\u5165&#xff09;&#xff0c;\u5426\u5219\u4e0d\u4f1a\u9677\u5165\u5185\u6838&#xff0c;\u56e0\u6b64\u901f\u5ea6\u975e\u5e38\u5feb\u3002<\/p>\n<\/li>\n<li>\n<p>\u4ec5\u9650\u5355\u8fdb\u7a0b\u5185\u4f7f\u7528&#xff1a;\u4e34\u754c\u533a\u4e0d\u80fd\u8de8\u8fdb\u7a0b\u540c\u6b65&#xff08;\u5982\u9700\u8de8\u8fdb\u7a0b&#xff0c;\u5e94\u4f7f\u7528\u4e92\u65a5\u4f53&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u53ef\u9012\u5f52&#xff08;\u53ef\u9009&#xff09;&#xff1a;\u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;\u540c\u4e00\u7ebf\u7a0b\u4e0d\u80fd\u591a\u6b21\u8fdb\u5165\u540c\u4e00\u4e2a\u4e34\u754c\u533a&#xff08;\u4f1a\u5bfc\u81f4\u6b7b\u9501&#xff09;&#xff0c;\u4f46\u53ef\u4ee5\u901a\u8fc7\u521d\u59cb\u5316\u65f6\u6307\u5b9a\u6807\u5fd7\u652f\u6301\u9012\u5f52\u3002<\/p>\n<\/li>\n<li>\n<p>\u65e0\u8d85\u65f6\u673a\u5236&#xff1a;EnterCriticalSection\u00a0\u4f1a\u4e00\u76f4\u7b49\u5f85\u76f4\u5230\u83b7\u5f97\u6240\u6709\u6743&#xff0c;\u65e0\u6cd5\u8bbe\u7f6e\u8d85\u65f6&#xff08;\u4f46\u53ef\u7528\u00a0TryEnterCriticalSection\u00a0\u5b9e\u73b0\u975e\u963b\u585e\u5c1d\u8bd5&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>2. \u4e34\u754c\u533a\u4e0e\u4e92\u65a5\u4f53\u7684\u5bf9\u6bd4<\/h3>\n<table>\n<tr>\u7279\u6027\u4e34\u754c\u533a\u4e92\u65a5\u4f53<\/tr>\n<tbody>\n<tr>\n<td>\u7c7b\u578b<\/td>\n<td>\u7528\u6237\u6001\u5bf9\u8c61<\/td>\n<td>\u5185\u6838\u5bf9\u8c61<\/td>\n<\/tr>\n<tr>\n<td>\u901f\u5ea6<\/td>\n<td>\u975e\u5e38\u5feb&#xff08;\u65e0\u5185\u6838\u5207\u6362&#xff09;<\/td>\n<td>\u8f83\u6162&#xff08;\u9700\u8fdb\u5165\u5185\u6838&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u8de8\u8fdb\u7a0b<\/td>\n<td>\u4e0d\u652f\u6301<\/td>\n<td>\u652f\u6301&#xff08;\u547d\u540d\u4e92\u65a5\u4f53&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u9012\u5f52<\/td>\n<td>\u9ed8\u8ba4\u4e0d\u652f\u6301&#xff0c;\u53ef\u542f\u7528<\/td>\n<td>\u652f\u6301<\/td>\n<\/tr>\n<tr>\n<td>\u8d85\u65f6\u7b49\u5f85<\/td>\n<td>\u4e0d\u652f\u6301&#xff08;\u53ef\u7528 TryEnter&#xff09;<\/td>\n<td>\u652f\u6301&#xff08;WaitForSingleObject&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u9057\u5f03\u68c0\u6d4b<\/td>\n<td>\u65e0<\/td>\n<td>\u652f\u6301&#xff08;WAIT_ABANDONED&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u9002\u7528\u8303\u56f4<\/td>\n<td>\u5355\u8fdb\u7a0b\u5185\u9ad8\u6027\u80fd\u540c\u6b65<\/td>\n<td>\u8de8\u8fdb\u7a0b\u3001\u9700\u8d85\u65f6\/\u9057\u5f03\u68c0\u6d4b\u7684\u573a\u666f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u9009\u62e9\u4f9d\u636e&#xff1a;\u5982\u679c\u53ea\u5728\u5355\u8fdb\u7a0b\u5185\u540c\u6b65&#xff0c;\u4e14\u8ffd\u6c42\u6700\u9ad8\u6027\u80fd&#xff0c;\u4e34\u754c\u533a\u662f\u9996\u9009\u3002\u82e5\u9700\u8981\u8de8\u8fdb\u7a0b\u3001\u8d85\u65f6\u6216\u68c0\u6d4b\u6240\u6709\u8005\u7ec8\u6b62&#xff0c;\u5219\u5e94\u4f7f\u7528\u4e92\u65a5\u4f53\u3002<\/p>\n<h3>3. \u6838\u5fc3 API \u51fd\u6570<\/h3>\n<h4>3.1 \u521d\u59cb\u5316\u4e34\u754c\u533a \u2014\u2014\u00a0InitializeCriticalSection<\/h4>\n<p>void InitializeCriticalSection(<br \/>\n  LPCRITICAL_SECTION lpCriticalSection  \/\/ \u6307\u5411 CRITICAL_SECTION \u7ed3\u6784\u7684\u6307\u9488<br \/>\n);<\/p>\n<ul>\n<li>\n<p>\u5728\u4f7f\u7528\u4e34\u754c\u533a\u4e4b\u524d\u5fc5\u987b\u8c03\u7528\u6b64\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/p>\n<\/li>\n<li>\n<p>CRITICAL_SECTION\u00a0\u662f\u4e00\u4e2a\u4e0d\u900f\u660e\u7684\u7ed3\u6784\u4f53&#xff0c;\u7531\u7cfb\u7edf\u5185\u90e8\u4f7f\u7528&#xff0c;\u5e94\u7528\u7a0b\u5e8f\u53ea\u9700\u5206\u914d\u5185\u5b58&#xff08;\u901a\u5e38\u4f5c\u4e3a\u5168\u5c40\u53d8\u91cf\u6216\u6210\u5458\u53d8\u91cf&#xff09;&#xff0c;\u65e0\u9700\u5173\u5fc3\u5176\u5185\u90e8\u7ec6\u8282\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u6269\u5c55&#xff1a;InitializeCriticalSectionAndSpinCount\u00a0\u5141\u8bb8\u8bbe\u7f6e\u81ea\u65cb\u8ba1\u6570&#xff0c;\u4f18\u5316\u591a\u6838 CPU \u4e0a\u7684\u6027\u80fd&#xff08;\u5728\u83b7\u53d6\u4e0d\u5230\u9501\u65f6\u5148\u81ea\u65cb\u7b49\u5f85\u4e00\u5c0f\u6bb5\u65f6\u95f4&#xff0c;\u907f\u514d\u7acb\u5373\u8fdb\u5165\u5185\u6838\u6001&#xff09;\u3002<\/p>\n<h4>3.2 \u8fdb\u5165\u4e34\u754c\u533a \u2014\u2014\u00a0EnterCriticalSection<\/h4>\n<p>void EnterCriticalSection(<br \/>\n  LPCRITICAL_SECTION lpCriticalSection<br \/>\n);<\/p>\n<ul>\n<li>\n<p>\u8c03\u7528\u7ebf\u7a0b\u5c1d\u8bd5\u83b7\u5f97\u4e34\u754c\u533a\u7684\u6240\u6709\u6743\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u4e34\u754c\u533a\u5f53\u524d\u672a\u88ab\u5360\u7528&#xff0c;\u7ebf\u7a0b\u7acb\u5373\u83b7\u5f97\u6240\u6709\u6743\u5e76\u8fdb\u5165\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u5df2\u88ab\u5176\u4ed6\u7ebf\u7a0b\u5360\u7528&#xff0c;\u8c03\u7528\u7ebf\u7a0b\u5c06\u963b\u585e&#xff08;\u8fdb\u5165\u7b49\u5f85\u72b6\u6001&#xff09;\u76f4\u5230\u5360\u7528\u8005\u8c03\u7528\u00a0LeaveCriticalSection\u3002<\/p>\n<\/li>\n<li>\n<p>\u6ce8\u610f&#xff1a;\u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;\u540c\u4e00\u7ebf\u7a0b\u591a\u6b21\u8c03\u7528\u00a0EnterCriticalSection\u00a0\u4f1a\u5bfc\u81f4\u6b7b\u9501&#xff08;\u9664\u975e\u521d\u59cb\u5316\u65f6\u542f\u7528\u4e86\u9012\u5f52&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<h4>3.3 \u5c1d\u8bd5\u8fdb\u5165\u4e34\u754c\u533a \u2014\u2014\u00a0TryEnterCriticalSection<\/h4>\n<p>BOOL TryEnterCriticalSection(<br \/>\n  LPCRITICAL_SECTION lpCriticalSection<br \/>\n);<\/p>\n<ul>\n<li>\n<p>\u975e\u963b\u585e\u7248\u672c&#xff1a;\u5c1d\u8bd5\u8fdb\u5165\u4e34\u754c\u533a&#xff0c;\u5982\u679c\u6210\u529f\u5219\u8fd4\u56de\u00a0TRUE\u00a0\u5e76\u62e5\u6709\u6240\u6709\u6743&#xff1b;\u5982\u679c\u5f53\u524d\u88ab\u5176\u4ed6\u7ebf\u7a0b\u5360\u7528&#xff0c;\u7acb\u5373\u8fd4\u56de\u00a0FALSE&#xff0c;\u4e0d\u4f1a\u963b\u585e\u3002<\/p>\n<\/li>\n<li>\n<p>\u9002\u7528\u4e8e\u9700\u8981\u201c\u5c1d\u8bd5\u6267\u884c&#xff0c;\u5426\u5219\u505a\u5176\u4ed6\u4e8b\u60c5\u201d\u7684\u573a\u666f\u3002<\/p>\n<\/li>\n<\/ul>\n<h4>3.4 \u79bb\u5f00\u4e34\u754c\u533a \u2014\u2014\u00a0LeaveCriticalSection<\/h4>\n<p>void LeaveCriticalSection(<br \/>\n  LPCRITICAL_SECTION lpCriticalSection<br \/>\n);<\/p>\n<ul>\n<li>\n<p>\u91ca\u653e\u4e34\u754c\u533a\u7684\u6240\u6709\u6743&#xff0c;\u5141\u8bb8\u5176\u4ed6\u7b49\u5f85\u7684\u7ebf\u7a0b\u8fdb\u5165\u3002<\/p>\n<\/li>\n<li>\n<p>\u8c03\u7528\u7ebf\u7a0b\u5fc5\u987b\u5df2\u7ecf\u62e5\u6709\u8be5\u4e34\u754c\u533a&#xff0c;\u5426\u5219\u884c\u4e3a\u672a\u5b9a\u4e49&#xff08;\u53ef\u80fd\u5bfc\u81f4\u6b7b\u9501\u6216\u5176\u4ed6\u7ebf\u7a0b\u65e0\u6cd5\u83b7\u5f97&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<h4>3.5 \u5220\u9664\u4e34\u754c\u533a \u2014\u2014\u00a0DeleteCriticalSection<\/h4>\n<p>void DeleteCriticalSection(<br \/>\n  LPCRITICAL_SECTION lpCriticalSection<br \/>\n);<\/p>\n<ul>\n<li>\n<p>\u5f53\u4e0d\u518d\u9700\u8981\u4e34\u754c\u533a\u65f6\u8c03\u7528&#xff0c;\u91ca\u653e\u7cfb\u7edf\u8d44\u6e90\u3002<\/p>\n<\/li>\n<li>\n<p>\u8c03\u7528\u524d\u5fc5\u987b\u786e\u4fdd\u6ca1\u6709\u7ebf\u7a0b\u6b63\u5728\u4f7f\u7528\u8be5\u4e34\u754c\u533a&#xff08;\u5373\u6240\u6709\u7ebf\u7a0b\u90fd\u5df2\u79bb\u5f00&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u5220\u9664\u540e\u4e0d\u80fd\u518d\u6b21\u4f7f\u7528\u8be5\u4e34\u754c\u533a&#xff0c;\u9664\u975e\u91cd\u65b0\u521d\u59cb\u5316\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>4. \u4f7f\u7528\u6b65\u9aa4<\/h3>\n<li>\n<p>\u5b9a\u4e49\u00a0CRITICAL_SECTION\u00a0\u53d8\u91cf&#xff08;\u901a\u5e38\u4f5c\u4e3a\u5168\u5c40\u6216\u9759\u6001\u53d8\u91cf&#xff0c;\u6216\u4f5c\u4e3a\u7c7b\u6210\u5458&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u521d\u59cb\u5316&#xff1a;\u8c03\u7528\u00a0InitializeCriticalSection\u3002<\/p>\n<\/li>\n<li>\n<p>\u8fdb\u5165\u4e34\u754c\u533a&#xff1a;\u5728\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u524d\u8c03\u7528\u00a0EnterCriticalSection\u3002<\/p>\n<\/li>\n<li>\n<p>\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90&#xff1a;\u6267\u884c\u9700\u8981\u4fdd\u62a4\u7684\u4ee3\u7801\u3002<\/p>\n<\/li>\n<li>\n<p>\u79bb\u5f00\u4e34\u754c\u533a&#xff1a;\u8bbf\u95ee\u5b8c\u6bd5\u540e\u8c03\u7528\u00a0LeaveCriticalSection\u3002<\/p>\n<\/li>\n<li>\n<p>\u5220\u9664\u4e34\u754c\u533a&#xff1a;\u7a0b\u5e8f\u7ed3\u675f\u6216\u4e0d\u518d\u9700\u8981\u65f6\u8c03\u7528\u00a0DeleteCriticalSection\u3002<\/p>\n<\/li>\n<h3>5. \u793a\u4f8b1&#xff1a;\u591a\u7ebf\u7a0b\u4fdd\u62a4\u5171\u4eab\u8ba1\u6570\u5668<\/h3>\n<p>\/\/ CriticalSectionExample.cpp<br \/>\n#include &lt;windows.h&gt;<br \/>\n#include &lt;stdio.h&gt;<\/p>\n<p>CRITICAL_SECTION g_cs;<br \/>\nint g_counter &#061; 0;<br \/>\nconst int ITERATIONS &#061; 1000000;<\/p>\n<p>DWORD WINAPI ThreadProc(LPVOID lpParam) {<br \/>\n    for (int i &#061; 0; i &lt; ITERATIONS; i&#043;&#043;) {<br \/>\n        EnterCriticalSection(&amp;g_cs);<br \/>\n        g_counter&#043;&#043;;  \/\/ \u4e34\u754c\u533a&#xff1a;\u4fdd\u62a4\u5171\u4eab\u53d8\u91cf<br \/>\n        LeaveCriticalSection(&amp;g_cs);<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    InitializeCriticalSection(&amp;g_cs);<\/p>\n<p>    HANDLE hThreads[4];<br \/>\n    for (int i &#061; 0; i &lt; 4; i&#043;&#043;) {<br \/>\n        hThreads[i] &#061; CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);<br \/>\n    }<\/p>\n<p>    WaitForMultipleObjects(4, hThreads, TRUE, INFINITE);<\/p>\n<p>    printf(&#034;Expected counter: %d\\\\n&#034;, 4 * ITERATIONS);<br \/>\n    printf(&#034;Actual counter: %d\\\\n&#034;, g_counter);<\/p>\n<p>    for (int i &#061; 0; i &lt; 4; i&#043;&#043;) CloseHandle(hThreads[i]);<br \/>\n    DeleteCriticalSection(&amp;g_cs);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>WaitForMultipleObjects\u00a0\u662f Windows API \u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u7b49\u5f85\u51fd\u6570&#xff0c;\u5b83\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b\u540c\u65f6\u76d1\u89c6\u591a\u4e2a\u5185\u6838\u5bf9\u8c61&#xff08;\u5982\u4e8b\u4ef6\u3001\u4e92\u65a5\u4f53\u3001\u4fe1\u53f7\u91cf\u3001\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u7b49&#xff09;\u7684\u72b6\u6001\u3002\u7ebf\u7a0b\u53ef\u4ee5\u963b\u585e&#xff0c;\u76f4\u5230\u4ee5\u4e0b\u4efb\u4e00\u6761\u4ef6\u6ee1\u8db3&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6307\u5b9a\u7684\u6240\u6709\u5bf9\u8c61\u90fd\u53d8\u4e3a\u6709\u4fe1\u53f7\u72b6\u6001&#xff08;\u7b49\u5f85\u5168\u90e8&#xff0c;bWaitAll &#061; TRUE&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u6307\u5b9a\u7684\u4efb\u4e00\u5bf9\u8c61\u53d8\u4e3a\u6709\u4fe1\u53f7\u72b6\u6001&#xff08;\u7b49\u5f85\u4efb\u4e00&#xff0c;bWaitAll &#061; FALSE&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u8be5\u51fd\u6570\u5e38\u7528\u4e8e\u9700\u8981\u540c\u65f6\u54cd\u5e94\u591a\u4e2a\u4e8b\u4ef6\u6216\u8d44\u6e90\u7684\u573a\u666f&#xff0c;\u4f8b\u5982\u7b49\u5f85\u591a\u4e2a\u7ebf\u7a0b\u5b8c\u6210\u3001\u7b49\u5f85\u591a\u4e2a\u4e8b\u4ef6\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u53d1\u751f\u7b49\u3002<\/p>\n<p>DWORD WaitForMultipleObjects(<br \/>\n  DWORD        nCount,           \/\/ \u5bf9\u8c61\u53e5\u67c4\u6570\u91cf<br \/>\n  const HANDLE *lpHandles,        \/\/ \u6307\u5411\u53e5\u67c4\u6570\u7ec4\u7684\u6307\u9488<br \/>\n  BOOL         bWaitAll,          \/\/ \u7b49\u5f85\u7c7b\u578b&#xff1a;TRUE&#061;\u5168\u90e8&#xff0c;FALSE&#061;\u4efb\u610f\u4e00\u4e2a<br \/>\n  DWORD        dwMilliseconds     \/\/ \u8d85\u65f6\u65f6\u95f4&#xff08;\u6beb\u79d2&#xff09;<br \/>\n);<\/p>\n<h4>\u53c2\u6570\u8be6\u89e3<\/h4>\n<ul>\n<li>\n<p>nCount&#xff1a;\u8981\u7b49\u5f85\u7684\u5bf9\u8c61\u53e5\u67c4\u6570\u91cf&#xff0c;\u53d6\u503c\u8303\u56f4\u4e3a 1 \u5230\u00a0MAXIMUM_WAIT_OBJECTS&#xff08;\u901a\u5e38\u4e3a 64&#xff09;\u3002\u4e0d\u80fd\u4e3a 0\u3002<\/p>\n<\/li>\n<li>\n<p>lpHandles&#xff1a;\u6307\u5411\u00a0HANDLE\u00a0\u7c7b\u578b\u6570\u7ec4\u7684\u6307\u9488&#xff0c;\u6570\u7ec4\u5305\u542b\u8981\u7b49\u5f85\u7684\u5185\u6838\u5bf9\u8c61\u53e5\u67c4\u3002\u8fd9\u4e9b\u53e5\u67c4\u53ef\u4ee5\u5c5e\u4e8e\u4e0d\u540c\u7c7b\u578b&#xff08;\u5982\u4e8b\u4ef6\u3001\u4e92\u65a5\u4f53\u3001\u8fdb\u7a0b\u7b49&#xff09;&#xff0c;\u4f46\u5fc5\u987b\u5177\u6709\u00a0SYNCHRONIZE\u00a0\u8bbf\u95ee\u6743\u9650\u3002<\/p>\n<\/li>\n<li>\n<p>bWaitAll&#xff1a;<\/p>\n<ul>\n<li>\n<p>TRUE&#xff1a;\u7b49\u5f85\u6240\u6709\u5bf9\u8c61\u90fd\u53d8\u4e3a\u6709\u4fe1\u53f7\u72b6\u6001\u3002\u51fd\u6570\u4ec5\u5728\u5168\u90e8\u5bf9\u8c61\u540c\u65f6\u6709\u4fe1\u53f7\u65f6\u624d\u8fd4\u56de\u3002<\/p>\n<\/li>\n<li>\n<p>FALSE&#xff1a;\u7b49\u5f85\u4efb\u4f55\u4e00\u4e2a\u5bf9\u8c61\u53d8\u4e3a\u6709\u4fe1\u53f7\u72b6\u6001\u3002\u51fd\u6570\u5728\u4efb\u4e00\u5bf9\u8c61\u6709\u4fe1\u53f7\u65f6\u7acb\u5373\u8fd4\u56de\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>dwMilliseconds&#xff1a;\u8d85\u65f6\u65f6\u95f4&#xff0c;\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\u3002<\/p>\n<ul>\n<li>\n<p>0&#xff1a;\u7acb\u5373\u68c0\u67e5\u5bf9\u8c61\u72b6\u6001\u5e76\u8fd4\u56de&#xff0c;\u4e0d\u963b\u585e\u3002<\/p>\n<\/li>\n<li>\n<p>INFINITE&#xff1a;\u65e0\u9650\u671f\u7b49\u5f85&#xff0c;\u76f4\u5230\u6761\u4ef6\u6ee1\u8db3\u3002<\/p>\n<\/li>\n<li>\n<p>\u5176\u4ed6\u503c&#xff1a;\u6700\u591a\u7b49\u5f85\u6307\u5b9a\u7684\u6beb\u79d2\u6570&#xff0c;\u8d85\u65f6\u540e\u8fd4\u56de\u00a0WAIT_TIMEOUT\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>\u8fd4\u56de\u503c<\/h4>\n<p>\u8fd4\u56de\u503c\u6307\u793a\u51fd\u6570\u8fd4\u56de\u7684\u539f\u56e0&#xff1a;<\/p>\n<ul>\n<li>\n<p>WAIT_OBJECT_0\u00a0\u5230\u00a0WAIT_OBJECT_0 &#043; nCount &#8211; 1&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5982\u679c\u00a0bWaitAll &#061; FALSE&#xff0c;\u8fd4\u56de\u503c\u4e3a\u00a0WAIT_OBJECT_0 &#043; i&#xff0c;\u8868\u793a\u6570\u7ec4\u4e2d\u7b2c\u00a0i\u00a0\u4e2a\u5bf9\u8c61\u53d8\u4e3a\u6709\u4fe1\u53f7\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u00a0bWaitAll &#061; TRUE&#xff0c;\u8fd4\u56de\u503c\u4e3a\u00a0WAIT_OBJECT_0&#xff0c;\u8868\u793a\u6240\u6709\u5bf9\u8c61\u90fd\u5df2\u53d8\u4e3a\u6709\u4fe1\u53f7&#xff08;\u5177\u4f53\u662f\u54ea\u4e2a\u5bf9\u8c61\u5148\u53d8\u7684\u4e0d\u91cd\u8981&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>WAIT_ABANDONED_0\u00a0\u5230\u00a0WAIT_ABANDONED_0 &#043; nCount &#8211; 1&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u4ec5\u9002\u7528\u4e8e\u7b49\u5f85\u4e92\u65a5\u4f53\u5bf9\u8c61\u3002\u8868\u793a\u7b49\u5f85\u6210\u529f&#xff0c;\u4f46\u5176\u4e2d\u81f3\u5c11\u6709\u4e00\u4e2a\u4e92\u65a5\u4f53\u88ab\u201c\u9057\u5f03\u201d&#xff08;\u5373\u62e5\u6709\u8be5\u4e92\u65a5\u4f53\u7684\u7ebf\u7a0b\u5728\u672a\u91ca\u653e\u7684\u60c5\u51b5\u4e0b\u7ec8\u6b62&#xff09;\u3002\u6b64\u65f6\u7ebf\u7a0b\u83b7\u5f97\u4e86\u4e92\u65a5\u4f53\u7684\u6240\u6709\u6743&#xff0c;\u4f46\u5171\u4eab\u8d44\u6e90\u53ef\u80fd\u5904\u4e8e\u4e0d\u4e00\u81f4\u72b6\u6001\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u00a0bWaitAll &#061; FALSE&#xff0c;\u8fd4\u56de\u503c\u4e3a\u00a0WAIT_ABANDONED_0 &#043; i&#xff0c;\u8868\u793a\u7b2c\u00a0i\u00a0\u4e2a\u4e92\u65a5\u4f53\u88ab\u9057\u5f03\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u00a0bWaitAll &#061; TRUE&#xff0c;\u8fd4\u56de\u00a0WAIT_ABANDONED_0&#xff0c;\u8868\u793a\u81f3\u5c11\u6709\u4e00\u4e2a\u4e92\u65a5\u4f53\u88ab\u9057\u5f03&#xff08;\u6240\u6709\u5bf9\u8c61\u90fd\u5df2\u6709\u4fe1\u53f7&#xff0c;\u4f46\u5176\u4e2d\u5305\u542b\u88ab\u9057\u5f03\u7684\u4e92\u65a5\u4f53&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>WAIT_TIMEOUT&#xff1a;\u8d85\u65f6\u65f6\u95f4\u5230\u8fbe&#xff0c;\u4e14\u6761\u4ef6\u672a\u6ee1\u8db3\u3002<\/p>\n<\/li>\n<li>\n<p>WAIT_FAILED&#xff1a;\u51fd\u6570\u8c03\u7528\u5931\u8d25&#xff0c;\u53ef\u901a\u8fc7\u00a0GetLastError\u00a0\u83b7\u53d6\u9519\u8bef\u4ee3\u7801\u3002\u5e38\u89c1\u9519\u8bef\u5982\u00a0nCount\u00a0\u8d85\u51fa\u9650\u5236\u3001\u53e5\u67c4\u65e0\u6548\u7b49\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>6. \u793a\u4f8b2&#xff1a;\u4fdd\u62a4\u590d\u6742\u6570\u636e\u7ed3\u6784&#xff08;\u53cc\u5411\u94fe\u8868&#xff09;<\/h3>\n<p>\/\/ CriticalSectionList.cpp<br \/>\n#include &lt;windows.h&gt;<br \/>\n#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<\/p>\n<p>typedef struct _Node {<br \/>\n    int data;<br \/>\n    struct _Node* next;<br \/>\n    struct _Node* prev;<br \/>\n} Node;<\/p>\n<p>Node* g_pHead &#061; NULL;<br \/>\nCRITICAL_SECTION g_csList;<\/p>\n<p>\/\/ \u63d2\u5165\u8282\u70b9&#xff08;\u9700\u8981\u540c\u6b65&#xff09;<br \/>\nvoid InsertNode(int value) {<br \/>\n    Node* pNew &#061; (Node*)malloc(sizeof(Node));<br \/>\n    pNew-&gt;data &#061; value;<\/p>\n<p>    EnterCriticalSection(&amp;g_csList);<br \/>\n    \/\/ \u63d2\u5165\u5230\u94fe\u8868\u5934\u90e8<br \/>\n    pNew-&gt;next &#061; g_pHead;<br \/>\n    pNew-&gt;prev &#061; NULL;<br \/>\n    if (g_pHead !&#061; NULL) {<br \/>\n        g_pHead-&gt;prev &#061; pNew;<br \/>\n    }<br \/>\n    g_pHead &#061; pNew;<br \/>\n    LeaveCriticalSection(&amp;g_csList);<br \/>\n}<\/p>\n<p>\/\/ \u5220\u9664\u7b2c\u4e00\u4e2a\u8282\u70b9&#xff08;\u9700\u8981\u540c\u6b65&#xff09;<br \/>\nint RemoveNode() {<br \/>\n    EnterCriticalSection(&amp;g_csList);<br \/>\n    if (g_pHead &#061;&#061; NULL) {<br \/>\n        LeaveCriticalSection(&amp;g_csList);<br \/>\n        return -1; \/\/ \u7a7a\u94fe\u8868<br \/>\n    }<br \/>\n    Node* pDel &#061; g_pHead;<br \/>\n    int value &#061; pDel-&gt;data;<br \/>\n    g_pHead &#061; pDel-&gt;next;<br \/>\n    if (g_pHead !&#061; NULL) {<br \/>\n        g_pHead-&gt;prev &#061; NULL;<br \/>\n    }<br \/>\n    LeaveCriticalSection(&amp;g_csList);<\/p>\n<p>    free(pDel);<br \/>\n    return value;<br \/>\n}<\/p>\n<p>DWORD WINAPI Producer(LPVOID) {<br \/>\n    for (int i &#061; 0; i &lt; 100; i&#043;&#043;) {<br \/>\n        InsertNode(i);<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>DWORD WINAPI Consumer(LPVOID) {<br \/>\n    for (int i &#061; 0; i &lt; 100; i&#043;&#043;) {<br \/>\n        int val &#061; RemoveNode();<br \/>\n        if (val !&#061; -1) {<br \/>\n            printf(&#034;Consumed: %d\\\\n&#034;, val);<br \/>\n        }<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    InitializeCriticalSection(&amp;g_csList);<\/p>\n<p>    HANDLE hThreads[2];<br \/>\n    hThreads[0] &#061; CreateThread(NULL, 0, Producer, NULL, 0, NULL);<br \/>\n    hThreads[1] &#061; CreateThread(NULL, 0, Consumer, NULL, 0, NULL);<\/p>\n<p>    WaitForMultipleObjects(2, hThreads, TRUE, INFINITE);<\/p>\n<p>    CloseHandle(hThreads[0]);<br \/>\n    CloseHandle(hThreads[1]);<br \/>\n    DeleteCriticalSection(&amp;g_csList);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u8bf4\u660e&#xff1a;\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u7ebf\u7a0b\u5e76\u53d1\u64cd\u4f5c\u94fe\u8868&#xff0c;\u4e34\u754c\u533a\u786e\u4fdd\u94fe\u8868\u64cd\u4f5c\u7684\u539f\u5b50\u6027\u3002<\/p>\n<h3>7. \u793a\u4f8b3&#xff1a;\u4f7f\u7528\u00a0TryEnterCriticalSection\u00a0\u907f\u514d\u6b7b\u9501<\/h3>\n<p>\u5f53\u9700\u8981\u540c\u65f6\u4fdd\u62a4\u591a\u4e2a\u8d44\u6e90\u65f6&#xff0c;\u4f7f\u7528\u00a0TryEnterCriticalSection\u00a0\u53ef\u4ee5\u4f18\u96c5\u5730\u5904\u7406\u83b7\u53d6\u4e0d\u5230\u9501\u7684\u60c5\u51b5&#xff0c;\u907f\u514d\u6b7b\u9501\u3002<\/p>\n<p>\/\/ TryEnterExample.cpp<br \/>\n#include &lt;windows.h&gt;<br \/>\n#include &lt;stdio.h&gt;<\/p>\n<p>CRITICAL_SECTION g_cs1, g_cs2;<\/p>\n<p>DWORD WINAPI Thread1(LPVOID) {<br \/>\n    EnterCriticalSection(&amp;g_cs1);<br \/>\n    printf(&#034;Thread1 got cs1\\\\n&#034;);<br \/>\n    Sleep(10); \/\/ \u6a21\u62df\u4e00\u4e9b\u5de5\u4f5c<\/p>\n<p>    \/\/ \u5c1d\u8bd5\u83b7\u53d6 cs2&#xff0c;\u82e5\u5931\u8d25\u5219\u91ca\u653e cs1 \u5e76\u91cd\u8bd5<br \/>\n    while (!TryEnterCriticalSection(&amp;g_cs2)) {<br \/>\n        printf(&#034;Thread1 cannot get cs2, releasing cs1 and retry&#8230;\\\\n&#034;);<br \/>\n        LeaveCriticalSection(&amp;g_cs1);<br \/>\n        Sleep(5); \/\/ \u8ba9\u51fa CPU<br \/>\n        EnterCriticalSection(&amp;g_cs1); \/\/ \u91cd\u65b0\u83b7\u53d6 cs1<br \/>\n        printf(&#034;Thread1 re-acquired cs1\\\\n&#034;);<br \/>\n    }<br \/>\n    printf(&#034;Thread1 got cs2\\\\n&#034;);<\/p>\n<p>    \/\/ \u540c\u65f6\u62e5\u6709\u4e24\u4e2a\u4e34\u754c\u533a&#xff0c;\u8fdb\u884c\u5de5\u4f5c<br \/>\n    LeaveCriticalSection(&amp;g_cs2);<br \/>\n    LeaveCriticalSection(&amp;g_cs1);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>DWORD WINAPI Thread2(LPVOID) {<br \/>\n    EnterCriticalSection(&amp;g_cs2);<br \/>\n    printf(&#034;Thread2 got cs2\\\\n&#034;);<br \/>\n    Sleep(10);<br \/>\n    EnterCriticalSection(&amp;g_cs1); \/\/ \u5982\u679c Thread1 \u6301\u6709 cs1&#xff0c;\u8fd9\u91cc\u4f1a\u6b7b\u9501&#xff1f;\u4f46 Thread1 \u4f7f\u7528\u4e86 TryEnter&#xff0c;\u53ef\u80fd\u907f\u514d<br \/>\n    printf(&#034;Thread2 got cs1\\\\n&#034;);<br \/>\n    LeaveCriticalSection(&amp;g_cs1);<br \/>\n    LeaveCriticalSection(&amp;g_cs2);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    InitializeCriticalSection(&amp;g_cs1);<br \/>\n    InitializeCriticalSection(&amp;g_cs2);<\/p>\n<p>    HANDLE hThreads[2];<br \/>\n    hThreads[0] &#061; CreateThread(NULL, 0, Thread1, NULL, 0, NULL);<br \/>\n    hThreads[1] &#061; CreateThread(NULL, 0, Thread2, NULL, 0, NULL);<\/p>\n<p>    WaitForMultipleObjects(2, hThreads, TRUE, INFINITE);<\/p>\n<p>    DeleteCriticalSection(&amp;g_cs1);<br \/>\n    DeleteCriticalSection(&amp;g_cs2);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u8bf4\u660e&#xff1a;Thread1 \u4f7f\u7528\u00a0TryEnterCriticalSection\u00a0\u5c1d\u8bd5\u83b7\u53d6\u7b2c\u4e8c\u4e2a\u9501&#xff0c;\u5982\u679c\u5931\u8d25\u5219\u91ca\u653e\u7b2c\u4e00\u4e2a\u9501\u5e76\u91cd\u8bd5&#xff0c;\u6253\u7834\u4e86\u53ef\u80fd\u53d1\u751f\u7684\u6b7b\u9501\u5faa\u73af\u3002<\/p>\n<h3>8. \u6ce8\u610f\u4e8b\u9879\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<li>\n<p>\u521d\u59cb\u5316\u4e0e\u5220\u9664\u5fc5\u987b\u914d\u5bf9&#xff1a;\u5fd8\u8bb0\u00a0DeleteCriticalSection\u00a0\u4f1a\u5bfc\u81f4\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e34\u754c\u533a\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f&#xff1a;\u5728\u8fdb\u5165\u4e34\u754c\u533a\u4e4b\u524d\u786e\u4fdd\u5df2\u7ecf\u521d\u59cb\u5316&#xff0c;\u5728\u79bb\u5f00\u540e\u786e\u4fdd\u6ca1\u6709\u7ebf\u7a0b\u518d\u4f7f\u7528\u5b83\u3002<\/p>\n<\/li>\n<li>\n<p>\u907f\u514d\u957f\u65f6\u95f4\u6301\u6709&#xff1a;\u4e34\u754c\u533a\u5185\u7684\u4ee3\u7801\u5e94\u5c3d\u91cf\u7b80\u77ed&#xff0c;\u51cf\u5c11\u5176\u4ed6\u7ebf\u7a0b\u7684\u7b49\u5f85\u65f6\u95f4\u3002<\/p>\n<\/li>\n<li>\n<p>\u5c0f\u5fc3\u5f02\u5e38&#xff1a;\u5728 C&#043;&#043; \u4e2d&#xff0c;\u5982\u679c\u4e34\u754c\u533a\u5185\u629b\u51fa\u5f02\u5e38\u4e14\u672a\u6355\u83b7&#xff0c;\u4f1a\u5bfc\u81f4\u00a0LeaveCriticalSection\u00a0\u4e0d\u88ab\u8c03\u7528&#xff0c;\u4ece\u800c\u6b7b\u9501\u3002\u53ef\u4f7f\u7528 RAII \u5305\u88c5\u7c7b&#xff08;\u5982\u00a0std::lock_guard\u00a0\u6216\u81ea\u5b9a\u4e49\u7c7b&#xff09;\u81ea\u52a8\u7ba1\u7406\u3002<\/p>\n<\/li>\n<li>\n<p>\u6b7b\u9501\u9884\u9632&#xff1a;\u5982\u679c\u9700\u8981\u591a\u4e2a\u4e34\u754c\u533a&#xff0c;\u5c3d\u91cf\u6309\u76f8\u540c\u987a\u5e8f\u83b7\u53d6&#xff1b;\u6216\u4f7f\u7528\u00a0TryEnterCriticalSection\u00a0\u5904\u7406\u83b7\u53d6\u5931\u8d25\u7684\u60c5\u51b5\u3002<\/p>\n<\/li>\n<li>\n<p>\u6027\u80fd\u8c03\u4f18&#xff1a;\u5bf9\u4e8e\u9ad8\u9891\u8bbf\u95ee\u4e14\u591a\u7ebf\u7a0b\u7ade\u4e89\u6fc0\u70c8\u7684\u573a\u666f&#xff0c;\u53ef\u8003\u8651\u4f7f\u7528\u00a0InitializeCriticalSectionAndSpinCount\u00a0\u8bbe\u7f6e\u81ea\u65cb\u8ba1\u6570&#xff0c;\u907f\u514d\u9891\u7e41\u8fdb\u5165\u5185\u6838\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e0d\u8981\u8de8\u8fdb\u7a0b\u4f7f\u7528&#xff1a;\u4e34\u754c\u533a\u5bf9\u8c61\u53ea\u80fd\u5728\u540c\u4e00\u8fdb\u7a0b\u5185\u6709\u6548&#xff0c;\u4e0d\u80fd\u901a\u8fc7\u5171\u4eab\u5185\u5b58\u7b49\u65b9\u5f0f\u7528\u4e8e\u8fdb\u7a0b\u95f4\u540c\u6b65\u3002<\/p>\n<\/li>\n<h2>\u8865\u5145\u77e5\u8bc6&#xff1a;<\/h2>\n<h3>1. \u4ec0\u4e48\u662f\u7528\u6237\u6001\u548c\u5185\u6838\u6001&#xff1f;<\/h3>\n<p>\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d&#xff0c;CPU \u7279\u6743\u7ea7\u522b\u901a\u5e38\u5206\u4e3a\u7528\u6237\u6001&#xff08;User Mode&#xff09;\u548c\u5185\u6838\u6001&#xff08;Kernel Mode&#xff09;&#xff08;\u6709\u7684\u67b6\u6784\u8fd8\u6709\u66f4\u591a\u7ea7\u522b&#xff09;\u3002\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u8fd0\u884c\u5728\u5185\u6838\u6001&#xff0c;\u62e5\u6709\u6700\u9ad8\u7684\u6743\u9650&#xff0c;\u53ef\u4ee5\u8bbf\u95ee\u6240\u6709\u786c\u4ef6\u548c\u7cfb\u7edf\u8d44\u6e90&#xff1b;\u800c\u666e\u901a\u5e94\u7528\u7a0b\u5e8f\u8fd0\u884c\u5728\u7528\u6237\u6001&#xff0c;\u6743\u9650\u53d7\u9650&#xff0c;\u5fc5\u987b\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\u8bf7\u6c42\u5185\u6838\u670d\u52a1\u3002<\/p>\n<ul>\n<li>\n<p>\u7528\u6237\u6001&#xff1a;\u5e94\u7528\u7a0b\u5e8f\u4ee3\u7801\u6267\u884c\u7684\u73af\u5883&#xff0c;\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u786c\u4ef6\u6216\u5185\u6838\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/li>\n<li>\n<p>\u5185\u6838\u6001&#xff1a;\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u4ee3\u7801\u6267\u884c\u7684\u73af\u5883&#xff0c;\u53ef\u4ee5\u6267\u884c\u7279\u6743\u6307\u4ee4&#xff0c;\u7ba1\u7406\u7cfb\u7edf\u8d44\u6e90\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u7cfb\u7edf\u8c03\u7528\u662f\u4ece\u7528\u6237\u6001\u8fdb\u5165\u5185\u6838\u6001\u7684\u552f\u4e00\u5408\u6cd5\u9014\u5f84&#xff08;\u9664\u786c\u4ef6\u4e2d\u65ad\u5916&#xff09;\u3002\u6bcf\u6b21\u7cfb\u7edf\u8c03\u7528\u90fd\u4f34\u968f\u7740\u4e0a\u4e0b\u6587\u5207\u6362\u548c\u6a21\u5f0f\u5207\u6362&#xff0c;\u6709\u4e00\u5b9a\u5f00\u9500\u3002<\/p>\n<h3>2. \u540c\u6b65\u5bf9\u8c61\u7684\u5185\u6838\u6001\u4e0e\u7528\u6237\u6001\u5206\u7c7b<\/h3>\n<p>Windows \u63d0\u4f9b\u7684\u540c\u6b65\u5bf9\u8c61\u53ef\u4ee5\u5206\u4e3a\u4e24\u7c7b&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5185\u6838\u5bf9\u8c61\u540c\u6b65&#xff1a;\u5982\u4e92\u65a5\u4f53&#xff08;Mutex&#xff09;\u3001\u4e8b\u4ef6&#xff08;Event&#xff09;\u3001\u4fe1\u53f7\u91cf&#xff08;Semaphore&#xff09;\u3001\u7b49\u5f85\u8ba1\u65f6\u5668&#xff08;Waitable Timer&#xff09;\u7b49\u3002\u8fd9\u4e9b\u5bf9\u8c61\u7531\u5185\u6838\u7ba1\u7406&#xff0c;\u7b49\u5f85\u64cd\u4f5c&#xff08;\u5982\u00a0WaitForSingleObject&#xff09;\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u8fdb\u5165\u5185\u6838\u6001\u7b49\u5f85&#xff0c;\u76f4\u5230\u6761\u4ef6\u6ee1\u8db3\u3002<\/p>\n<\/li>\n<li>\n<p>\u7528\u6237\u6001\u540c\u6b65&#xff1a;\u5982\u4e34\u754c\u533a&#xff08;Critical Section&#xff09;\u3001\u8f7b\u91cf\u7ea7\u8bfb\u5199\u9501&#xff08;SRWLock&#xff09;\u3001\u6761\u4ef6\u53d8\u91cf&#xff08;Condition Variable&#xff0c;\u914d\u5408 SRWLock&#xff09;\u7b49\u3002\u8fd9\u4e9b\u5bf9\u8c61\u4e3b\u8981\u5728\u7528\u6237\u6001\u64cd\u4f5c&#xff0c;\u53ea\u6709\u5728\u53d1\u751f\u7ade\u4e89&#xff08;\u7b49\u5f85&#xff09;\u65f6\u624d\u53ef\u80fd\u77ed\u6682\u8fdb\u5165\u5185\u6838\u3002<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1. \u4e34\u754c\u533a\u6982\u8ff0\u4e34\u754c\u533a&#xff08;Critical Section&#xff09;\u662f Windows \u63d0\u4f9b\u7684\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\u540c\u6b65\u5bf9\u8c61&#xff0c;\u7528\u4e8e\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\u5728\u540c\u4e00\u8fdb\u7a0b\u5185\u7684\u591a\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u4e92\u65a5\u8bbf\u95ee\u3002\u5b83\u786e\u4fdd\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8fdb\u5165\u4e34\u754c\u533a\u4ee3\u7801\u6bb5&#xff0c;\u4ece\u800c\u907f\u514d\u6570\u636e\u7ade\u4e89\u548c\u4e0d\u4e00\u81f4\u6027\u3002\u6838\u5fc3\u7279\u70b9\u7528\u6237\u6001\u540c\u6b65&#xff1a;\u4e34\u754c\u533a\u5bf9\u8c61\u5728\u7528\u6237\u6001\u5b9e\u73b0\u540c\u6b65&#xff0c;\u9664\u975e\u53d1\u751f\u7ade\u4e89&#xff08;\u5373\u591a\u4e2a\u7ebf\u7a0b\u5c1d\u8bd5\u540c\u65f6\u8fdb\u5165&#xff09;&#xff0c;\u5426\u5219\u4e0d\u4f1a\u9677\u5165\u5185\u6838&amp;#xff0c<\/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":[149,371,305],"topic":[],"class_list":["post-78563","post","type-post","status-publish","format-standard","hentry","category-server","tag-windows","tag-371","tag-305"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Windows\u5b66\u4e60\u7b14\u8bb0-20\uff08\u7ebf\u7a0b\u540c\u6b65-\u4e34\u754c\u533aCritical Section\uff09 - \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\/78563.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Windows\u5b66\u4e60\u7b14\u8bb0-20\uff08\u7ebf\u7a0b\u540c\u6b65-\u4e34\u754c\u533aCritical Section\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"1. \u4e34\u754c\u533a\u6982\u8ff0\u4e34\u754c\u533a&#xff08;Critical Section&#xff09;\u662f Windows \u63d0\u4f9b\u7684\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\u540c\u6b65\u5bf9\u8c61&#xff0c;\u7528\u4e8e\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\u5728\u540c\u4e00\u8fdb\u7a0b\u5185\u7684\u591a\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u4e92\u65a5\u8bbf\u95ee\u3002\u5b83\u786e\u4fdd\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8fdb\u5165\u4e34\u754c\u533a\u4ee3\u7801\u6bb5&#xff0c;\u4ece\u800c\u907f\u514d\u6570\u636e\u7ade\u4e89\u548c\u4e0d\u4e00\u81f4\u6027\u3002\u6838\u5fc3\u7279\u70b9\u7528\u6237\u6001\u540c\u6b65&#xff1a;\u4e34\u754c\u533a\u5bf9\u8c61\u5728\u7528\u6237\u6001\u5b9e\u73b0\u540c\u6b65&#xff0c;\u9664\u975e\u53d1\u751f\u7ade\u4e89&#xff08;\u5373\u591a\u4e2a\u7ebf\u7a0b\u5c1d\u8bd5\u540c\u65f6\u8fdb\u5165&#xff09;&#xff0c;\u5426\u5219\u4e0d\u4f1a\u9677\u5165\u5185\u6838&amp;#xff0c\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/78563.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-27T14:29:24+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=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/78563.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/78563.html\",\"name\":\"Windows\u5b66\u4e60\u7b14\u8bb0-20\uff08\u7ebf\u7a0b\u540c\u6b65-\u4e34\u754c\u533aCritical Section\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-27T14:29:24+00:00\",\"dateModified\":\"2026-02-27T14:29:24+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/78563.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/78563.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/78563.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Windows\u5b66\u4e60\u7b14\u8bb0-20\uff08\u7ebf\u7a0b\u540c\u6b65-\u4e34\u754c\u533aCritical Section\uff09\"}]},{\"@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":"Windows\u5b66\u4e60\u7b14\u8bb0-20\uff08\u7ebf\u7a0b\u540c\u6b65-\u4e34\u754c\u533aCritical Section\uff09 - \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\/78563.html","og_locale":"zh_CN","og_type":"article","og_title":"Windows\u5b66\u4e60\u7b14\u8bb0-20\uff08\u7ebf\u7a0b\u540c\u6b65-\u4e34\u754c\u533aCritical Section\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"1. \u4e34\u754c\u533a\u6982\u8ff0\u4e34\u754c\u533a&#xff08;Critical Section&#xff09;\u662f Windows \u63d0\u4f9b\u7684\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\u540c\u6b65\u5bf9\u8c61&#xff0c;\u7528\u4e8e\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\u5728\u540c\u4e00\u8fdb\u7a0b\u5185\u7684\u591a\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u4e92\u65a5\u8bbf\u95ee\u3002\u5b83\u786e\u4fdd\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8fdb\u5165\u4e34\u754c\u533a\u4ee3\u7801\u6bb5&#xff0c;\u4ece\u800c\u907f\u514d\u6570\u636e\u7ade\u4e89\u548c\u4e0d\u4e00\u81f4\u6027\u3002\u6838\u5fc3\u7279\u70b9\u7528\u6237\u6001\u540c\u6b65&#xff1a;\u4e34\u754c\u533a\u5bf9\u8c61\u5728\u7528\u6237\u6001\u5b9e\u73b0\u540c\u6b65&#xff0c;\u9664\u975e\u53d1\u751f\u7ade\u4e89&#xff08;\u5373\u591a\u4e2a\u7ebf\u7a0b\u5c1d\u8bd5\u540c\u65f6\u8fdb\u5165&#xff09;&#xff0c;\u5426\u5219\u4e0d\u4f1a\u9677\u5165\u5185\u6838&amp;#xff0c","og_url":"https:\/\/www.wsisp.com\/helps\/78563.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-27T14:29:24+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/78563.html","url":"https:\/\/www.wsisp.com\/helps\/78563.html","name":"Windows\u5b66\u4e60\u7b14\u8bb0-20\uff08\u7ebf\u7a0b\u540c\u6b65-\u4e34\u754c\u533aCritical Section\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-27T14:29:24+00:00","dateModified":"2026-02-27T14:29:24+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/78563.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/78563.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/78563.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Windows\u5b66\u4e60\u7b14\u8bb0-20\uff08\u7ebf\u7a0b\u540c\u6b65-\u4e34\u754c\u533aCritical Section\uff09"}]},{"@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\/78563","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=78563"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/78563\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=78563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=78563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=78563"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=78563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}