C++多線程開發(fā)環(huán)境配置方法
下載安裝 MinGW-w64

必須選擇帶 “posix” 的版本,這是多線程支持的關(guān)鍵!
解壓到 C:\mingw64(路徑不要有空格和中文)
配置環(huán)境變量
右鍵"此電腦" → 屬性 → 高級系統(tǒng)設(shè)置 → 環(huán)境變量
在 系統(tǒng)變量 中找到 Path → 編輯 → 新建 → 添加:
C:\mingw64\bin
驗(yàn)證安裝
按 Win+R,輸入 cmd 回車,執(zhí)行:
g++ --version
下載安裝VS code
安裝完成后安裝 VS Code 擴(kuò)展:
打開 VS Code,點(diǎn)擊左側(cè)擴(kuò)展圖標(biāo)(四方塊),搜索并安裝:
C/C++ - Microsoft(必裝)
C/C++ Extension Pack - Microsoft(推薦)
安裝后 重啟 VS Code。
創(chuàng)建測試項(xiàng)目
在桌面新建文件夾 cpp-thread-test
用 VS Code 打開該文件夾(文件 → 打開文件夾)
新建文件 main.cpp,粘貼以下代碼:
#include <iostream>
#include <thread>
#include <vector>
#include <mutex>
std::mutex mtx;
void print_thread_id(int id) {
std::lock_guard<std::mutex> lock(mtx);
std::cout << "Thread " << id << " is running\n";
}
int main() {
std::vector<std::thread> threads;
for (int i = 0; i < 5; ++i) {
threads.emplace_back(print_thread_id, i);
}
for (auto& th : threads) {
th.join();
}
std::cout << "All threads completed!" << std::endl;
system("pause"); // 防止閃退
return 0;
}配置編譯任務(wù)
創(chuàng)建 tasks.json
按 Ctrl+Shift+P,輸入 “Tasks: Configure Default Build Task” → 選擇 “Create tasks.json file from template” → 選擇 “Others”。
將生成的文件內(nèi)容 全部替換 為:
{
"version": "2.0.0",
"tasks": [
{
"label": "build C++ multi-thread",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-std=c++17",
"-pthread"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"],
"detail": "編譯帶線程支持的 C++ 程序"
}
]
}關(guān)鍵參數(shù)說明:
-g:生成調(diào)試信息
-std=c++17:使用 C++17 標(biāo)準(zhǔn)
-pthread:啟用多線程支持
創(chuàng)建 launch.json
點(diǎn)擊左側(cè)運(yùn)行和調(diào)試圖標(biāo)(??)→ 點(diǎn)擊 “創(chuàng)建 launch.json 文件” → 選擇 “C++ (GDB/LLDB)” → 選擇 “g++.exe - 生成和調(diào)試活動文件”。
將生成的文件內(nèi)容 全部替換 為:
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Debug Multi-thread",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true, // 使用外部控制臺防止閃退
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe", // 按實(shí)際路徑修改
"setupCommands": [
{
"description": "為 gdb 啟用整齊打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build C++ multi-thread"
}
]
}手工配置編譯任務(wù)
如果在上一步的配置調(diào)試設(shè)置
這里沒有看到: 選擇 “g++.exe - 生成和調(diào)試活動文件”。
因?yàn)?VS Code 沒檢測到 g++ 編譯器。終極解決方案:手動創(chuàng)建配置
目文件夾里,找到 .vscode 文件夾,刪除里面所有文件(launch.json, tasks.json 等)
創(chuàng)建 tasks.json
右鍵 .vscode 文件夾 → 新建文件 → 命名為 tasks.json → 粘貼:
{
"version": "2.0.0",
"tasks": [
{
"label": "build C++ multi-thread",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-std=c++17",
"-pthread"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"]
}
]
}創(chuàng)建 launch.json
右鍵 .vscode 文件夾 → 新建文件 → 命名為 launch.json → 粘貼:
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Debug Multi-thread",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "為 gdb 啟用整齊打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build C++ multi-thread"
}
]
}新建項(xiàng)目時,直接復(fù)制整個 .vscode 文件夾到新項(xiàng)目根目錄
到此這篇關(guān)于C++多線程開發(fā)環(huán)境配置方法的文章就介紹到這了,更多相關(guān)C++多線程環(huán)境內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用C語言實(shí)現(xiàn)圣誕樹(簡易版+進(jìn)階版)
大家好,本篇文章主要講的是用C語言實(shí)現(xiàn)圣誕樹(簡易版+進(jìn)階版),感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
C語言編程動態(tài)內(nèi)存開辟實(shí)現(xiàn)升級版通訊錄教程示例
這篇文章主要為大家介紹了C語言編程實(shí)現(xiàn)動態(tài)內(nèi)存開辟升級版通訊錄的教程示例及解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10
Qt中關(guān)聯(lián)容器QMap,QMultiMap,QHash,QMultiHash的使用
本文主要介紹了Qt中關(guān)聯(lián)容器QMap,QMultiMap,QHash,QMultiHash的使用,這些關(guān)聯(lián)容器在Qt中提供了靈活而強(qiáng)大的數(shù)據(jù)結(jié)構(gòu)選項(xiàng),根據(jù)具體的需求和使用場景,您可以選擇適合的容器來存儲和管理數(shù)據(jù),感興趣的可以了解一下2023-09-09
C++二叉搜索樹及其實(shí)現(xiàn)方法實(shí)例代碼
這篇文章主要介紹了C++二叉搜索樹及其實(shí)現(xiàn)方法的相關(guān)資料,搜索二叉樹是一種左小右大的二叉樹結(jié)構(gòu),支持高效查找、插入和刪除操作,需要的朋友可以參考下2025-06-06
浮點(diǎn)數(shù)乘法和整形乘除法的效率經(jīng)驗(yàn)比較
這篇文章主要為大家介紹了浮點(diǎn)數(shù)乘法和整形乘除法的效率經(jīng)驗(yàn)比較,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05

