{"id":8732,"date":"2023-02-03T00:23:48","date_gmt":"2023-02-02T16:23:48","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=8732"},"modified":"2024-05-05T14:18:24","modified_gmt":"2024-05-05T06:18:24","slug":"%e5%a4%9a%e7%ba%bf%e7%a8%8b%e4%bd%bf%e7%94%a8-windows-api%ef%bc%88%e7%bc%96%e8%be%91%e4%b8%ad%ef%bc%89","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=8732","title":{"rendered":"\u591a\u7ebf\u7a0b\u4f7f\u7528 &#8211; Windows API"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title ez-toc-toggle\" style=\"cursor:pointer\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.iyatt.com\/?p=8732\/#%E7%8E%AF%E5%A2%83\" >\u73af\u5883<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.iyatt.com\/?p=8732\/#%E4%BD%BF%E7%94%A8%E5%A4%9A%E7%BA%BF%E7%A8%8B\" >\u4f7f\u7528\u591a\u7ebf\u7a0b<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.iyatt.com\/?p=8732\/#%E7%BA%BF%E7%A8%8B%E5%90%8C%E6%AD%A5\" >\u7ebf\u7a0b\u540c\u6b65<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.iyatt.com\/?p=8732\/#%E5%8E%9F%E5%AD%90%E9%94%81\" >\u539f\u5b50\u9501<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.iyatt.com\/?p=8732\/#%E4%BA%92%E6%96%A5%E9%94%81\" >\u4e92\u65a5\u9501<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.iyatt.com\/?p=8732\/#%E4%BA%8B%E4%BB%B6\" >\u4e8b\u4ef6<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blog.iyatt.com\/?p=8732\/#%E4%BF%A1%E5%8F%B7%E9%87%8F\" >\u4fe1\u53f7\u91cf<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%E7%8E%AF%E5%A2%83\"><\/span>\u73af\u5883<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Windows 11 \u4e13\u4e1a\u5de5\u4f5c\u7ad9\u7248 22H2<br \/>\nVisual Studio 2022 \u4e13\u4e1a\u7248<\/p>\n<p>Unicode \u5b57\u7b26\u96c6\uff1b<br \/>\nC17 \u6807\u51c6\uff0c64 \u4f4d\u7f16\u8bd1<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E4%BD%BF%E7%94%A8%E5%A4%9A%E7%BA%BF%E7%A8%8B\"><\/span>\u4f7f\u7528\u591a\u7ebf\u7a0b<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2023\/02\/image-1675354689306.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 445px; --smush-placeholder-aspect-ratio: 445\/377;\" \/><\/p>\n<p>\u6211\u5199\u7684\u793a\u4f8b\u4ee3\u7801\u4e2d\u521b\u5efa\u4e86\u4e09\u4e2a\u5b50\u7ebf\u7a0b\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u8fd0\u884c\u65f6\u95f4 5 \u79d2\uff0c\u6bcf\u79d2\u8f93\u51fa\u4e00\u6b21\u81ea\u5df1\u7684\u5e8f\u53f7\uff08\u6309\u521b\u5efa\u987a\u5e8f\u7f16\u7684\u53f7\uff09\uff0c\u7ebf\u7a0b ID \u4ee5\u53ca\u6267\u884c\u6b21\u53f7\u3002<br \/>\n\u4e3b\u7ebf\u7a0b\u6bcf\u521b\u5efa\u5b8c\u4e00\u4e2a\u5b50\u7ebf\u7a0b\uff0c\u5bf9\u5e94\u5b50\u7ebf\u7a0b\u5c31\u5df2\u7ecf\u5f00\u59cb\u8fd0\u884c\uff0c\u521b\u5efa\u5b8c 3 \u4e2a\u5b50\u7ebf\u7a0b\u540e\uff0c\u4e3b\u7ebf\u7a0b\u6302\u8d77 3 \u79d2\uff0c\u9192\u6765\u540e\u5c06\u7ebf\u7a0b 0 \u6302\u8d77\uff0c\u5176\u5b83\u4e24\u4e2a\u7ebf\u7a0b\u662f\u7ee7\u7eed\u8fd0\u884c\u7684\uff0c\u4e3b\u7ebf\u7a0b\u81ea\u5df1\u53c8\u6302\u8d77 3 \u79d2\uff0c\u8fd9\u6b21\u9192\u6765\u540e\u53c8\u5c06\u7ebf\u7a0b 0 \u5524\u9192\u3002\u7ebf\u7a0b 0 \u9192\u6765\u7684\u65f6\u5019\u5df2\u7ecf\u662f\u7b2c 6 \u79d2\u4e86\uff0c\u53e6\u5916\u4e24\u4e2a\u7ebf\u7a0b\u5728\u7b2c 5 \u79d2\u7684\u65f6\u5019\u5c31\u7ed3\u675f\u4e86\u3002\u7ebf\u7a0b 0 \u6700\u5f00\u59cb\u6267\u884c\u4e86 3 \u79d2\uff0c\u6240\u4ee5\u518d\u6267\u884c 2 \u79d2\u540e\uff0c\u4e5f\u7ed3\u675f\u8fd0\u884c\u4e86\u3002<br \/>\n\u6700\u540e\u4e00\u884c\u8f93\u51fa\u7684 3 \u4e2a\u6570\u5b57\u662f\u4ece\u4e3b\u7ebf\u7a0b\u4e2d\u83b7\u53d6\u5230\u521b\u5efa\u7684\u5b50\u7ebf\u7a0b\u7684 ID\u3002<\/p>\n<pre><code class=\"language-c\">#include &lt;Windows.h&gt;\n#include &lt;stdio.h&gt;\n\n#define NUMBER_OF_THREADS   3 \/\/ \u521b\u5efa\u7ebf\u7a0b\u6570\n\nHANDLE ghStdOutput = NULL; \/\/ \u5b58\u653e\u63a7\u5236\u53f0\u6807\u51c6\u8f93\u51fa\u53e5\u67c4\n\n\/**\n * @brief \u7ebf\u7a0b\u51fd\u6570\n * @param lpParam \u521b\u5efa\u7ebf\u7a0b\u51fd\u6570\u7684\u7b2c\u56db\u4e2a\u53c2\u6570\u4f1a\u4f20\u5230\u8fd9\u91cc\u6765\n * @return \n*\/\nDWORD WINAPI fnThread(LPVOID lpParam)\n{\n    WCHAR wcsOutput[128] = {0};\n\n    for (int i = 0; i &lt; 5; ++i)\n    {\n        memset(wcsOutput, 0, sizeof(wcsOutput));\n        wsprintf(wcsOutput, L&quot;\u7ebf\u7a0b\u5e8f\u53f7 = %d\uff0c\u5f53\u524d\u7ebf\u7a0b ID = %d\uff0c\u5f53\u524d\u6267\u884c\u6b21\u53f7 = %d\\n&quot;, *(PINT)lpParam, GetCurrentThreadId(), i);\n        WriteConsole(ghStdOutput, wcsOutput, wcslen(wcsOutput), NULL, NULL);\n\n        Sleep(1000);\n    }\n    wsprintf(wcsOutput, L&quot;\u7ebf\u7a0b %d \u7ed3\u675f\\n&quot;, *(PINT)lpParam);\n    WriteConsole(ghStdOutput, wcsOutput, wcslen(wcsOutput), NULL, NULL);\n\n    return 0;\n}\n\nint main()\n{\n    HANDLE hStdError = GetStdHandle(STD_ERROR_HANDLE); \/\/ \u83b7\u53d6\u63a7\u5236\u53f0\u6807\u51c6\u9519\u8bef\u53e5\u67c4\n    ghStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);\n    PINT pThreadNumber[NUMBER_OF_THREADS] = {NULL}; \/\/ \u7528\u4e8e\u5b58\u653e\u4f20\u5165\u7ebf\u7a0b\u7684\u6570\u636e\n    HANDLE hNewThreadHandle[NUMBER_OF_THREADS] = {NULL}; \/\/ \u7528\u4e8e\u5b58\u653e\u521b\u5efa\u7684\u65b0\u7ebf\u7a0b\u7684\u53e5\u67c4\n    DWORD dwNewThreadID[NUMBER_OF_THREADS] = {0}; \/\/ \u7528\u4e8e\u5b58\u653e\u521b\u5efa\u7684\u7ebf\u7a0b\u7684 ID\n\n    for (int i = 0; i &lt; NUMBER_OF_THREADS; ++i)\n    {\n        \/\/ \u4ece\u5806\u4e2d\u5206\u914d\u5185\u5b58\n        pThreadNumber[i] = (PINT)HeapAlloc(GetProcessHeap(), \/\/ \u4ece\u5f53\u524d\u8fdb\u7a0b\u7684\u5806\u4e2d\u5206\u914d\u5185\u5b58\n                                           HEAP_ZERO_MEMORY, \/\/ \u5206\u914d\u7684\u5185\u5b58\u5168\u90e8\u521d\u59cb\u5316\u4e3a\u96f6\n                                           sizeof(PINT)); \/\/ \u8981\u5206\u914d\u7684\u5b57\u8282\u6570\n        if (pThreadNumber[i] == NULL)\n        {\n            WriteConsole(hStdError, L&quot;pThreadNumber \u5206\u914d\u5185\u5b58\u5931\u8d25\uff01\\n&quot;, wcslen(L&quot;pThreadNumber \u5206\u914d\u5185\u5b58\u5931\u8d25\uff01\\n&quot;), NULL, NULL);\n            ExitProcess(1);\n        }\n\n        *pThreadNumber[i] = i; \/\/ \u5b58\u5165\u7ebf\u7a0b\u5e8f\u53f7\n\n        hNewThreadHandle[i] = CreateThread(NULL, \/\/ \u9ed8\u8ba4\u5b89\u5168\u5c5e\u6027\n                                        0, \/\/ \u9ed8\u8ba4\u6808\u5927\u5c0f\n                                        fnThread, \/\/ \u7ebf\u7a0b\u51fd\u6570\u540d\n                                        pThreadNumber[i], \/\/ \u7ebf\u7a0b\u51fd\u6570\u7684\u53c2\u6570\n                                        0, \/\/ \u4f7f\u7528\u9ed8\u8ba4\u521b\u5efa\u6807\u5fd7\n                                        &amp;dwNewThreadID[i]); \/\/ \u8fd4\u56de\u7ebf\u7a0b ID\n        if (!hNewThreadHandle[i])\n        {\n            WriteConsole(hStdError, L&quot;hNewThreadHandle \u521b\u5efa\u7ebf\u7a0b\u5931\u8d25\uff01\\n&quot;, wcslen(L&quot;hNewThreadHandle \u521b\u5efa\u7ebf\u7a0b\u5931\u8d25\uff01\\n&quot;), NULL, NULL);\n            ExitProcess(2);\n        }\n    }\n\n    Sleep(3000); \/\/ \u4e3b\u7ebf\u7a0b\u6302\u8d77\u4e09\u79d2\u540e\u82cf\u9192\n    SuspendThread(hNewThreadHandle[0]); \/\/ \u5c06\u7b2c\u4e00\u4e2a\u5b50\u7ebf\u7a0b\u6302\u8d77\n    WriteConsole(ghStdOutput, L&quot;\u7ebf\u7a0b 0 \u6302\u8d77\\n&quot;, wcslen(L&quot;\u7ebf\u7a0b 0 \u6302\u8d77\\n&quot;), NULL, NULL);\n    Sleep(3000);\n    ResumeThread(hNewThreadHandle[0]); \/\/ \u5524\u9192\u7b2c\u4e00\u4e2a\u5b50\u7ebf\u7a0b\n    WriteConsole(ghStdOutput, L&quot;\u7ebf\u7a0b 0 \u82cf\u9192\\n&quot;, wcslen(L&quot;\u7ebf\u7a0b 0 \u82cf\u9192\\n&quot;), NULL, NULL);\n\n    \/\/ \u7b49\u5f85\u5b50\u7ebf\u7a0b\u7ed3\u675f\uff0c\u518d\u89e3\u9664\u4e3b\u7ebf\u7a0b\u963b\u585e\uff0c\u4fdd\u8bc1\u4e3b\u7ebf\u7a0b\u6700\u540e\u9000\u51fa\n    WaitForMultipleObjects(NUMBER_OF_THREADS, \/\/ \u7b49\u5f85\u7ebf\u7a0b\u6570\u91cf\n                            hNewThreadHandle, \/\/ \u7b49\u5f85\u7684\u7ebf\u7a0b\u53e5\u67c4\n                            TRUE, \/\/ \u7b49\u5f85\u524d\u4e00\u4e2a\u53c2\u6570\u4e2d\u6240\u6709\u53e5\u67c4\u5bf9\u5e94\u7684\u7ebf\u7a0b\u53d1\u51fa\u4fe1\u53f7\u65f6\uff0c\u672c\u51fd\u6570\u624d\u89e3\u9664\u963b\u585e\n                            INFINITE); \/\/ \u4e00\u76f4\u7b49\u5f85\n\n    \/\/ \u67e5\u770b\u8fd4\u56de\u7684\u7ebf\u7a0b ID\n    for (int i = 0; i &lt; NUMBER_OF_THREADS; ++i)\n    {\n        wprintf(L&quot;%d &quot;, dwNewThreadID[i]);\n    }\n    wprintf(L&quot;\\n&quot;);\n\n    \/\/ \u5584\u540e\u5904\u7406\n    for (int i = 0; i &lt; NUMBER_OF_THREADS; ++i)\n    {\n        CloseHandle(hNewThreadHandle[i]); \/\/ \u5173\u95ed\u7ebf\u7a0b\u53e5\u67c4\n        if (pThreadNumber[i])\n        {\n            HeapFree(GetProcessHeap(), 0, pThreadNumber[i]); \/\/ \u91ca\u653e\u7533\u8bf7\u7684\u5806\u7a7a\u95f4\n            pThreadNumber[i] = NULL;\n        }\n\n        return 0;\n    }\n}<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"%E7%BA%BF%E7%A8%8B%E5%90%8C%E6%AD%A5\"><\/span>\u7ebf\u7a0b\u540c\u6b65<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"%E5%8E%9F%E5%AD%90%E9%94%81\"><\/span>\u539f\u5b50\u9501<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u591a\u4e2a\u7ebf\u7a0b\u5bf9\u540c\u4e00\u4e2a\u6570\u636e\u6267\u884c\u539f\u5b50\u64cd\u4f5c\uff0c\u4f1a\u4ea7\u751f\u7ed3\u679c\u4e22\u5931\uff0c\u6bd4\u5982\u81ea\u589e\uff08\u5148\u4ece\u5185\u5b58\u5c06\u53d8\u91cf\u503c\u8bfb\u5165\u5bc4\u5b58\u5668\uff0c\u7136\u540e\u5728\u5bc4\u5b58\u5668\u4e2d\u52a0\u4e00\u64cd\u4f5c\uff0c\u6700\u540e\u5c06\u7ed3\u679c\u5199\u56de\u5185\u5b58\uff09<br \/>\n\u5047\u5982\u4e00\u4e2a\u7ebf\u7a0b\u521a\u4ece\u5185\u5b58\u8bfb\u53d6\u4e86\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u5230\u5bc4\u5b58\u5668\u4e2d\u5c31\u5931\u53bb\u4e86 CPU \u6267\u884c\u6743\uff0c\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u540c\u4e00\u4e2a\u53d8\u91cf\u64cd\u4f5c\uff0c\u6070\u597d\u6b64\u65f6\u83b7\u5f97 CPU \u6267\u884c\u6743\uff0c\u5e76\u505a\u5b8c\u4e86\u4e00\u6b21\u81ea\u589e\uff0c\u7136\u540e\u53c8\u5931\u53bb\u4e86\u6267\u884c\u6743\u9650\uff0c\u6b64\u65f6\u539f\u5148\u7684\u7ebf\u7a0b\u53c8\u83b7\u5f97\u4e86 CPU \u6267\u884c\u6743\uff0c\u7ee7\u7eed\u4e0a\u6b21\u7684\u5de5\u4f5c\uff0c\u4f7f\u7528\u6700\u521d\u8bfb\u53d6\u7684\u503c\u8fdb\u884c\u52a0\u4e00\u64cd\u4f5c\u518d\u5199\u56de\u5185\u5b58\u3002\u4e24\u4e2a\u7ebf\u7a0b\u5404\u6267\u884c\u4e00\u6b21\uff0c\u4f46\u662f\u90fd\u662f\u5bf9\u6700\u521d\u503c\u52a0\u4e00\uff0c\u800c\u4e0d\u662f\u5728\u5176\u4e2d\u4e00\u4e2a\u7ebf\u7a0b\u81ea\u589e\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\uff0c\u90a3\u4e48\u95ee\u9898\u5c31\u4ea7\u751f\u4e86\u3002<br \/>\n\u6240\u6709\u7ebf\u7a0b\u5bf9\u540c\u4e00\u4e2a\u53d8\u91cf\u8fdb\u884c\u4fee\u6539\u7684\u5730\u65b9\u52a0\u4e0a\u539f\u5b50\u9501\u540e\uff0c\u5f53\u67d0\u4e2a\u7ebf\u7a0b\u51c6\u5907\u4fee\u6539\u53d8\u91cf\u65f6\uff0c\u4f1a\u5148\u68c0\u67e5\u662f\u5426\u5df2\u7ecf\u4e0a\u9501\uff0c\u5df2\u7ecf\u4e0a\u9501\u5c31\u963b\u585e\uff0c\u6ca1\u6709\u4e0a\u9501\u5c31\u7ed9\u53d8\u91cf\u4e0a\u9501\uff0c\u5f53\u8fd9\u4e2a\u7ebf\u7a0b\u5bf9\u53d8\u91cf\u4e0a\u9501\u540e\uff0c\u5176\u5b83\u7ebf\u7a0b\u8bd5\u56fe\u4fee\u6539\u65f6\u4e5f\u4f1a\u5224\u65ad\u662f\u5426\u4e0a\u9501\uff0c\u53ea\u8981\u5f53\u524d\u7ebf\u7a0b\u6ca1\u5b8c\u6210\u4fee\u6539\u64cd\u4f5c\u5c31\u4e0d\u4f1a\u89e3\u9501\uff0c\u5176\u5b83\u7ebf\u7a0b\u5c31\u4f1a\u4e00\u76f4\u963b\u585e\uff0c\u76f4\u5230\u5b8c\u6210\u4fee\u6539\u89e3\u5f00\u9501\uff0c\u5176\u5b83\u7ebf\u7a0b\u624d\u80fd\u7ee7\u7eed\u4fee\u6539\u3002<br \/>\n\u5f53\u7136\u8fd9\u6837\u64cd\u4f5c\u7684\u8bdd\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u5728\u4fee\u6539\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u5176\u5b83\u8bd5\u56fe\u5728\u6b64\u65f6\u4fee\u6539\u540c\u4e00\u53d8\u91cf\u7684\u7ebf\u7a0b\u5c31\u4f1a\u88ab\u963b\u585e\u5728\u90a3\u91cc\uff0c\u5b9e\u9645\u5c31\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u5728\u8fd0\u884c\u3002\u4ee5\u964d\u4f4e\u6548\u7387\u4e3a\u4ee3\u4ef7\u6362\u53d6\u7a0b\u5e8f\u6b63\u786e\u8fd0\u884c\u3002<\/p>\n<p>\u8fd9\u91cc\u5199\u4e86\u4e00\u4e2a\u4f8b\u5b50\uff0c\u4e24\u4e2a\u5b50\u7ebf\u7a0b\u5bf9\u540c\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u5404\u81ea\u81ea\u589e(\u81ea\u51cf) 100000 \u6b21<br \/>\n\u7b2c\u4e00\u4e2a\u6570\u662f\u76f4\u63a5\u81ea\u589e 100000 \u6b21\uff0c\u7b2c\u4e8c\u4e2a\u662f\u4f7f\u7528\u4e86\u539f\u5b50\u9501\u81ea\u589e\uff1b\u7b2c\u4e09\u4e2a\u662f\u81ea\u51cf 100000 \u6b21\uff0c\u7b2c\u56db\u4e2a\u662f\u4f7f\u7528\u4e86\u539f\u5b50\u9501\u7684\u81ea\u51cf\u3002<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2023\/02\/image-1675389795725.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 99px; --smush-placeholder-aspect-ratio: 99\/100;\" \/><\/p>\n<p>\u4e0d\u8fc7\u4f7f\u7528\u539f\u5b50\u9501\u6bd4\u8f83\u9ebb\u70e6\uff0c\u6bcf\u4e2a\u64cd\u4f5c\u7b26\u90fd\u5bf9\u5e94\u4e00\u4e2a\u51fd\u6570\uff0c\u4e0d\u662f\u7ecf\u5e38\u7528\u4f30\u8ba1\u90fd\u8bb0\u4e0d\u4f4f\uff0c\u8981\u53bb\u73b0\u67e5\u6587\u6863\u3002\u5c40\u9650\u6027\uff0c\u539f\u5b50\u9501\u53ea\u80fd\u5bf9\u53d8\u91cf\u8fdb\u884c\u64cd\u4f5c\u7b26\u8fd0\u7b97\u3002<\/p>\n<pre><code class=\"language-c\">#include &lt;Windows.h&gt;\n#include &lt;stdio.h&gt;\n\nINT gIcre = 0;\nINT gLockedIncre = 0;\nINT gDecre = 200000;\nINT gLockedDecre = 200000;\n\nDWORD WINAPI fnThread(LPVOID lpParam)\n{\n    for (int i = 0; i &lt; 100000; ++i)\n    {\n        ++gIcre;\n        InterlockedIncrement(&amp;gLockedIncre); \/\/ \u539f\u5b50\u9501\u9012\u52a0\n        --gDecre;\n        InterlockedDecrement(&amp;gLockedDecre); \/\/ \u539f\u5b50\u9501\u9012\u51cf\n    }\n\n    return 0;\n}\n\nint main()\n{\n    HANDLE hStdError = GetStdHandle(STD_ERROR_HANDLE);\n    HANDLE hNewThreadHandle[2] = {NULL};\n\n    if (!(hNewThreadHandle[0] = CreateThread(NULL, 0, fnThread, NULL, 0, NULL)))\n    {\n        WriteConsole(hStdError, L&quot;\u521b\u5efa\u7ebf\u7a0b 0 \u5931\u8d25\\n&quot;, wcslen(L&quot;\u521b\u5efa\u7ebf\u7a0b 0 \u5931\u8d25\\n&quot;), NULL, NULL);\n        ExitProcess(1);\n    }\n    if (!(hNewThreadHandle[1] = CreateThread(NULL, 0, fnThread, NULL, 0, NULL)))\n    {\n        WriteConsole(hStdError, L&quot;\u521b\u5efa\u7ebf\u7a0b 1 \u5931\u8d25\\n&quot;, wcslen(L&quot;\u521b\u5efa\u7ebf\u7a0b 0 \u5931\u8d25\\n&quot;), NULL, NULL);\n        ExitProcess(1);\n    }\n\n    WaitForMultipleObjects(2, hNewThreadHandle, TRUE, INFINITE);\n\n    wprintf(L&quot;%d\\n&quot;, gIcre);\n    wprintf(L&quot;%d\\n&quot;, gLockedIncre);\n    wprintf(L&quot;%d\\n&quot;, gDecre);\n    wprintf(L&quot;%d\\n&quot;, gLockedDecre);\n\n    CloseHandle(hNewThreadHandle[0]);\n    CloseHandle(hNewThreadHandle[1]);\n}<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"%E4%BA%92%E6%96%A5%E9%94%81\"><\/span>\u4e92\u65a5\u9501<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u8fd9\u91cc\u5199\u4e86\u4e00\u4e2a\u4f8b\u5b50\uff0c\u4e24\u4e2a\u7ebf\u7a0b\uff0c\u5176\u4e2d\u4e00\u4e2a\u7ebf\u7a0b\u8f93\u51fa *\uff0c\u6bcf\u8f93\u51fa 20 \u4e2a\u6362\u4e00\u6b21\u884c\uff0c\u6362\u884c 10 \u6b21\uff0c\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b\u8f93\u51fa\u7684\u7b26\u53f7\u662f &#8211;<br \/>\n\u4f46\u662f\u8fd0\u884c\u7ed3\u679c\u5e76\u4e0d\u662f\u9884\u60f3\u7684<\/p>\n<pre><code>********************\n--------------------\n********************\n\u3002\u3002\u3002 \u8fd9\u4e2a\u6837\u5b50<\/code><\/pre>\n<p>\u5b9e\u9645\u662f<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2023\/02\/image-1675411918482.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 367px; --smush-placeholder-aspect-ratio: 367\/330;\" \/><\/p>\n<pre><code class=\"language-c\">#include &lt;Windows.h&gt;\n#include &lt;stdio.h&gt;\n\nDWORD WINAPI fnThread1(LPVOID lpParam)\n{\n    for (int i = 0; i &lt; 10; ++i)\n    {\n        for (int j = 0; j &lt; 20; ++j)\n        {\n            wprintf(L&quot;-&quot;);\n            Sleep(10);\n        }\n        wprintf(L&quot;\\n&quot;);\n    }\n    return 0;\n}\n\nDWORD WINAPI fnThread2(LPVOID lpParam)\n{\n    for (int i = 0; i &lt; 10; ++i)\n    {\n        for (int j = 0; j &lt; 20; ++j)\n        {\n            wprintf(L&quot;*&quot;);\n            Sleep(10);\n        }\n        wprintf(L&quot;\\n&quot;);\n    }\n    return 0;\n}\n\nint main()\n{\n    HANDLE hThreadHandle[2] = {NULL};\n\n    hThreadHandle[0] = CreateThread(NULL, 0, fnThread1, NULL, 0, NULL);\n    hThreadHandle[1] = CreateThread(NULL, 0, fnThread2, NULL, 0, NULL);\n\n    WaitForMultipleObjects(2, hThreadHandle, TRUE, INFINITE);\n\n    CloseHandle(hThreadHandle[0]);\n    CloseHandle(hThreadHandle[1]);\n\n    return 0;\n}<\/code><\/pre>\n<p>\u8fd9\u6b21\u7684\u8d44\u6e90\u4e0d\u518d\u662f\u53d8\u91cf\uff0c\u800c\u662f\u63a7\u5236\u53f0\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u5411\u540c\u4e00\u4e2a\u63a7\u5236\u53f0\u5199\u4e1c\u897f\u663e\u793a\u51fa\u6765\u3002\u5728\u64cd\u4f5c\u7cfb\u7edf\u7684\u8c03\u5ea6\u4e0b\uff0c\u7cfb\u7edf\u4e2d\u5404\u4e2a\u7ebf\u7a0b\u8f6e\u6d41\u4f7f\u7528 CPU\uff0c\u5c31\u5bfc\u81f4\u67d0\u4e2a\u7ebf\u7a0b\u7684\u4e00\u6bb5\u5de5\u4f5c\u4e0d\u80fd\u8fde\u7eed\u8fdb\u884c\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u90fd\u662f\u5de5\u4f5c\u4e00\u4f1a\u505c\u4e00\u4f1a\uff0c\u6700\u7ec8\u663e\u793a\u7ed3\u679c\u5c31\u662f * \u548c &#8211; \u4ea4\u53c9\u5728\u4e00\u8d77\u3002<\/p>\n<p>\u8981\u60f3\u8fbe\u5230\u9884\u60f3\u7684\u6548\u679c\uff0c\u8fd9\u91cc\u53ef\u4ee5\u4f7f\u7528\u4e92\u65a5\u9501\uff0c\u5c06\u9700\u8981\u8fde\u7eed\u5411\u63a7\u5236\u53f0\u8f93\u51fa\u7684\u90e8\u5206\u52a0\u9501\uff0c\u5f53\u9501\u88ab\u67d0\u4e2a\u7ebf\u7a0b\u83b7\u53d6\u540e\uff0c\u5176\u5b83\u7ebf\u7a0b\u8bd5\u56fe\u83b7\u53d6\u9501\u65f6\u53d1\u73b0\u65e0\u6cd5\u83b7\u53d6\u5c31\u4f1a\u963b\u585e\u5728\u90a3\u91cc\uff0c\u7b49\u5f85\u5df2\u7ecf\u83b7\u53d6\u9501\u7684\u7ebf\u7a0b\u6267\u884c\u5b8c\u5e76\u91ca\u653e\u9501\uff0c\u5176\u5b83\u7ebf\u7a0b\u624d\u53ef\u7ee7\u7eed\u53c2\u4e0e\u83b7\u53d6\u9501\u3002\u5f53\u7136\u8fd9\u6837\u4e5f\u4f1a\u6709\u6027\u80fd\u635f\u5931\uff0c\u5f53\u51e0\u4e2a\u7ebf\u7a0b\u90fd\u5230\u4e86\u8981\u4f7f\u7528\u67d0\u4e2a\u8d44\u6e90\u7684\u65f6\u5019\uff0c\u53ea\u6709\u83b7\u53d6\u5230\u4e92\u65a5\u9501\u7684\u80fd\u5de5\u4f5c\uff0c\u5176\u5b83\u90fd\u4f1a\u88ab\u963b\u585e\u5728\u90a3\u91cc\uff0c\u4e00\u4e2a\u4e00\u4e2a\u8f6e\u6d41\u4f7f\u7528\u90a3\u4e2a\u8d44\u6e90\u3002<\/p>\n<p>\u52a0\u4e0a\u4e92\u65a5\u9501\u540e<\/p>\n<pre><code class=\"language-c\">#include &lt;Windows.h&gt;\n#include &lt;stdio.h&gt;\n\nHANDLE g_mutex; \/\/ \u5b58\u50a8\u4e92\u65a5\u9501\u53e5\u67c4\n\nDWORD WINAPI fnThread1(LPVOID lpParam)\n{\n    for (int i = 0; i &lt; 10; ++i)\n    {\n        WaitForSingleObject(g_mutex, INFINITE); \/\/ \u7b49\u5f85\u4e92\u65a5\u9501\uff0c\u6ca1\u7b49\u5230\u5c31\u4e00\u76f4\u963b\u585e\n        for (int j = 0; j &lt; 20; ++j)\n        {\n            wprintf(L&quot;-&quot;);\n            Sleep(10);\n        }\n        wprintf(L&quot;\\n&quot;);\n        ReleaseMutex(g_mutex); \/\/ \u91ca\u653e\u4e92\u65a5\u9501\n    }\n    return 0;\n}\n\nDWORD WINAPI fnThread2(LPVOID lpParam)\n{\n    for (int i = 0; i &lt; 10; ++i)\n    {\n        WaitForSingleObject(g_mutex, INFINITE);\n        for (int j = 0; j &lt; 20; ++j)\n        {\n            wprintf(L&quot;*&quot;);\n            Sleep(10);\n        }\n        wprintf(L&quot;\\n&quot;);\n        ReleaseMutex(g_mutex);\n    }\n    return 0;\n}\n\nint main()\n{\n    HANDLE hThreadHandle[2] = {NULL};\n    g_mutex = CreateMutex(NULL, FALSE, NULL); \/\/ \u521b\u5efa\u4e92\u65a5\u9501\n    HANDLE outputHandle = GetStdHandle(STD_OUTPUT_HANDLE);\n\n    hThreadHandle[0] = CreateThread(NULL, 0, fnThread1, NULL, 0, NULL);\n    hThreadHandle[1] = CreateThread(NULL, 0, fnThread2, NULL, 0, NULL);\n\n    WaitForMultipleObjects(2, hThreadHandle, TRUE, INFINITE);\n\n    CloseHandle(hThreadHandle[0]);\n    CloseHandle(hThreadHandle[1]);\n    CloseHandle(outputHandle);\n\n    return 0;\n}<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2023\/02\/image-1675413609204.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 223px; --smush-placeholder-aspect-ratio: 223\/354;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E4%BA%8B%E4%BB%B6\"><\/span>\u4e8b\u4ef6<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u8fd9\u91cc\u5199\u4e86\u4e00\u4e2a\u4f8b\u5b50\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u7528\u4e24\u4e2a\u4e8b\u4ef6\u5206\u522b\u63a7\u5236\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b\u5b9a\u65f6\u8f93\u51fa\u4e00\u53e5\u8bdd\u548c\u7ebf\u7a0b\u9000\u51fa\u3002<br \/>\n\u8d1f\u8d23\u63a7\u5236\u7684\u7ebf\u7a0b\uff0c\u6bcf\u79d2\u8bbe\u7f6e\u4e00\u6b21\u4e8b\u4ef6\uff0c\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b\u4f1a\u4e00\u76f4\u7b49\u5f85\u8fd9\u4e2a\u4e8b\u4ef6\uff0c\u7b49\u4e0d\u5230\u5c31\u963b\u585e\uff0c\u7b49\u5230\u4e86\u5c31\u8fd4\u56de\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\u3002\u53e6\u5916\u8d1f\u8d23\u63a7\u5236\u7684\u7ebf\u7a0b\u5728\u7b2c 4 \u79d2\u8bbe\u7f6e\u53e6\u5916\u4e00\u4e2a\u4e8b\u4ef6\uff0c\u5728\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u4e5f\u5728\u7b49\u8fd9\u4e2a\u4e8b\u4ef6\uff0c\u4f46\u662f\u53ea\u7b49\u4e00\u6beb\u79d2\uff0c\u8d85\u65f6\u5c31\u8fd4\u56de\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\uff0c\u7136\u540e\u8fd4\u56de\u503c\u5224\u65ad\u662f\u5426\u7b49\u5230\u4e86\u4e8b\u4ef6\uff0c\u7b49\u5230\u4e86\u5c31\u6267\u884c\u9000\u51fa\u3002<\/p>\n<pre><code class=\"language-c\">#include &lt;Windows.h&gt;\n\nHANDLE g_timerEvent, g_quitEvent; \/\/ \u5b58\u50a8\u4e8b\u4ef6\u7684\u53e5\u67c4\n\nDWORD WINAPI threadFunction1(LPVOID lpParam)\n{\n    HANDLE outputHandle = GetStdHandle(STD_OUTPUT_HANDLE);\n    while (TRUE)\n    {\n        WaitForSingleObject(g_timerEvent, INFINITE); \/\/ \u7b49\u5f85\u4e8b\u4ef6\uff0c\u6ca1\u7b49\u5230\u5c31\u4e00\u76f4\u963b\u585e\uff0c\u7b49\u5230\u5c31\u8fd4\u56de\n        if (WaitForSingleObject(g_quitEvent, 1) == WAIT_OBJECT_0) \/\/ \u7b49\u5f85 1 ms\uff0c\u5224\u65ad\u662f\u5426\u7b49\u5230\u4e8b\u4ef6\uff0c\u7b49\u5230\u5c31\u9000\u51fa\u8fd0\u884c\n        {\n            WriteConsole(outputHandle, L&quot;\u9000\u51fa\\n&quot;, wcslen(L&quot;\u9000\u51fa\\n&quot;), NULL, NULL);\n            CloseHandle(outputHandle);\n            break;\n        }\n        WriteConsole(outputHandle, L&quot;\u5b9a\u65f6\u4e8b\u4ef6\\n&quot;, wcslen(L&quot;\u5b9a\u65f6\u4e8b\u4ef6\\n&quot;), NULL, NULL);\n        ResetEvent(g_timerEvent); \/\/ \u590d\u4f4d\u4e8b\u4ef6\n    }\n    return 0;\n}\n\nDWORD WINAPI threadFunction2(LPVOID lpParam)\n{\n    INT counter = 0;\n    while (TRUE)\n    {\n        Sleep(1000);\n        SetEvent(g_timerEvent); \/\/ \u5b9a\u65f6\u8bbe\u7f6e\u4e8b\u4ef6\uff0c\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b\u6839\u636e\u4e8b\u4ef6\u63a7\u5236\u8fd0\u884c\u6216\u963b\u585e\n        if (counter == 4)\n        {\n            SetEvent(g_quitEvent); \/\/ \u7b2c 4 \u79d2\u7684\u65f6\u5019\u8bbe\u7f6e\u4e8b\u4ef6\uff0c\u63a7\u5236\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b\u9000\u51fa\n            break;\n        }\n        ++counter;\n    }\n    return 0;\n}\n\nint main()\n{\n    \/\/ \u521b\u5efa\u4e8b\u4ef6\n    g_timerEvent = CreateEvent(NULL, \/\/ \u5b89\u5168\u5c5e\u6027\n                                TRUE, \/\/ \u624b\u52a8\u590d\u4f4d\n                                FALSE, \/\/ \u521d\u59cb\u72b6\u6001\u65e0\u4fe1\u53f7\n                                NULL); \/\/ \u4e8b\u4ef6\u547d\u540d\n    g_quitEvent = CreateEvent(NULL, TRUE, FALSE, NULL);\n\n    HANDLE threadHandle[2] = {NULL};\n\n    threadHandle[0] = CreateThread(NULL, 0, threadFunction1, NULL, 0, NULL);\n    threadHandle[1] = CreateThread(NULL, 0, threadFunction2, NULL, 0, NULL);\n\n    WaitForMultipleObjects(2, threadHandle, TRUE, INFINITE);\n\n    CloseHandle(threadHandle[0]);\n    CloseHandle(threadHandle[1]);\n    CloseHandle(g_timerEvent);\n    CloseHandle(g_quitEvent);\n\n    return 0;\n}<\/code><\/pre>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2023\/02\/image-1675425926033.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 138px; --smush-placeholder-aspect-ratio: 138\/132;\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E4%BF%A1%E5%8F%B7%E9%87%8F\"><\/span>\u4fe1\u53f7\u91cf<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u8fd9\u91cc\u5199\u4e86\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5148\u521b\u5efa\u4e00\u4e2a\u4fe1\u53f7\u91cf\uff0c\u521d\u59cb\u8ba1\u6570\u4e3a 5\uff0c\u5b50\u7ebf\u7a0b\u6bcf\u7b49\u5f85\u4e00\u6b21\u4fe1\u53f7\u91cf\uff0c\u4fe1\u53f7\u91cf\u4e2d\u7684\u8ba1\u6570\u5c31\u4f1a\u51cf\u4e00\uff0c\u51cf\u4e3a 0 \u540e\u7b49\u5f85\u5c31\u4f1a\u963b\u585e\u3002\u5b50\u7ebf\u7a0b\u6bcf\u6b21\u6267\u884c\u540e\u6302\u8d77 100 \u6beb\u79d2\uff0c\u521d\u59cb 5 \u6b21\u5927\u6982\u82b1 500+ \u6beb\u79d2\u3002\u5728\u5b50\u7ebf\u7a0b\u5f00\u59cb\u6267\u884c\u540e\uff0c\u4e3b\u7ebf\u7a0b\u6302\u8d77 600 \u6beb\u79d2\uff0c\u7136\u540e\u7ed9\u4fe1\u53f7\u91cf\u8ba1\u6570\u518d\u589e\u52a0 3 \u6b21\u3002\u5b50\u7ebf\u7a0b\u7b49\u5f85\u5230\u65b0\u589e\u7684\u8ba1\u6570\u53c8\u53ef\u4ee5\u7b49\u5f85\u8fc7 3 \u6b21\uff0c\u4e4b\u540e\u53c8\u963b\u585e\u3002\u6700\u540e\u56e0\u4e3a\u6211\u8bbe\u7f6e\u7684\u7b49\u5f85\u65f6\u95f4\u662f 2 \u79d2\uff0c\u7136\u540e for \u5faa\u73af\u5269\u4e0b\u7684\u4e24\u6b21\u6bcf\u6b21\u90fd\u8981\u7b49\u8fc7\u4e24\u79d2\u8d85\u65f6\u6267\u884c\uff0c\u6700\u540e\u7ed3\u675f\u8fd0\u884c\u3002<\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2023\/02\/image-1675431342063.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 142px; --smush-placeholder-aspect-ratio: 142\/213;\" \/><\/p>\n<pre><code class=\"language-c\">#include &lt;Windows.h&gt;\n#include &lt;stdio.h&gt;\n\nHANDLE g_semaphoreHandle; \/\/ \u5b58\u653e\u4fe1\u53f7\u91cf\u53e5\u67c4\n\nDWORD WINAPI threadFunction(LPVOID lpParam)\n{\n    for (int i = 0; i &lt; 10; ++i)\n    {\n        WaitForSingleObject(g_semaphoreHandle, 2000); \/\/ \u6bcf\u7b49\u5f85\u4e00\u6b21\uff0c\u8ba1\u6570\u51cf\u4e00\uff0c\u4e3a 0 \u963b\u585e\u3002\u6700\u591a\u7b49\u5f85 2 \u79d2\uff0c\u8d85\u65f6\u4e5f\u4f1a\u8fd4\u56de\n        wprintf(L&quot;%d\\n&quot;, i);\n        Sleep(100);\n    }\n    return 0;\n}\n\nint main()\n{\n     g_semaphoreHandle = CreateSemaphore(NULL, \/\/ \u5b89\u5168\u5c5e\u6027\uff0c\u9ed8\u8ba4\n                                         5, \/\/ \u4fe1\u53f7\u91cf\u521d\u59cb\u8ba1\u6570\n                                         10, \/\/ \u6700\u5927\u8ba1\u6570\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e0d\u80fd\u8d85\u8fc7\u8fd9\u4e2a\u503c\n                                         NULL); \/\/ \u4e3a\u4fe1\u53f7\u91cf\u53d6\u540d\n    HANDLE threadHandle = CreateThread(NULL, 0, threadFunction, NULL, 0, NULL);\n    Sleep(600);\n    ReleaseSemaphore(g_semaphoreHandle,\n                     3, \/\/ \u8ba1\u6570\u589e\u52a0 3\u3002\u589e\u52a0\u540e\u7684\u603b\u8ba1\u6570\u4e0d\u80fd\u8d85\u8fc7\u521b\u5efa\u65f6\u8bbe\u7f6e\u7684\u6700\u5927\u8ba1\u6570\uff0c\u5426\u5219\u65e0\u6548\n                     NULL); \/\/ \u63a5\u6536\u4e0a\u4e00\u4e2a\u8ba1\u6570\uff0c\u4e0d\u9700\u8981\u8bbe\u7f6e NULL\n    WaitForSingleObject(threadHandle, INFINITE);\n    CloseHandle(threadHandle);\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u73af\u5883 Windows 11 \u4e13\u4e1a\u5de5\u4f5c\u7ad9\u7248 22H2 Visual Studio 2022 \u4e13\u4e1a\u7248 Unicod [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"zakra_page_container_layout":"customizer","zakra_page_sidebar_layout":"customizer","zakra_remove_content_margin":false,"zakra_sidebar":"customizer","zakra_transparent_header":"customizer","zakra_logo":0,"zakra_main_header_style":"default","zakra_menu_item_color":"","zakra_menu_item_hover_color":"","zakra_menu_item_active_color":"","zakra_menu_active_style":"","zakra_page_header":true,"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[1,593],"tags":[],"class_list":["post-8732","post","type-post","status-publish","format-standard","hentry","category-all","category-windows-api"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/8732","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8732"}],"version-history":[{"count":0,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/8732\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}