在现代软件开发中,C++ thread库 是实现多线程编程和提升程序性能的重要工具。无论你是刚接触 C++ 的新手,还是希望深入理解 C++并发机制的开发者,本篇 thread使用教程 都将带你从基础到进阶,全面掌握 C++11 及以后标准中提供的线程功能。

C++11 标准引入了 <thread> 头文件,提供了对操作系统原生线程的封装,使开发者无需依赖平台特定的 API(如 Windows 的 CreateThread 或 POSIX 的 pthread)即可编写跨平台的多线程程序。
使用 thread 库,你可以轻松地创建、管理线程,并通过互斥锁、条件变量等同步机制协调多个线程之间的操作。
下面是一个最简单的 C++ 多线程示例:
#include <iostream>#include <thread>void hello() { std::cout << "Hello from thread!\n";}int main() { std::thread t(hello); // 创建一个新线程,执行 hello 函数 t.join(); // 等待线程执行完毕 std::cout << "Main thread continues.\n"; return 0;}在这个例子中:
std::thread t(hello) 创建了一个新线程,该线程会调用 hello() 函数。t.join() 表示主线程会等待子线程执行完成后再继续执行。⚠️ 注意:如果不调用join()或detach(),程序会在std::thread对象析构时调用std::terminate(),导致程序异常终止!
调用 join() 后,当前线程会阻塞,直到目标线程执行完毕。这是最常用的方式,确保资源安全释放。
detach() 将线程与主线程分离,使其在后台独立运行。主线程不再等待它结束,但需注意:被 detach 的线程不能访问主线程的局部变量(可能已销毁)。
#include <iostream>#include <thread>#include <chrono>void background_task() { std::this_thread::sleep_for(std::chrono::seconds(2)); std::cout << "Background task done!\n";}int main() { std::thread t(background_task); t.detach(); // 分离线程 std::cout << "Main thread exits immediately.\n"; std::this_thread::sleep_for(std::chrono::seconds(3)); // 确保能看到输出 return 0;}你可以在创建线程时传递参数:
#include <iostream>#include <thread>void print_sum(int a, int b) { std::cout << a << " + " << b << " = " << (a + b) << "\n";}int main() { std::thread t(print_sum, 10, 20); t.join(); return 0;}注意:默认情况下参数是按值拷贝传递的。如果需要引用传递,必须使用 std::ref():
#include <iostream>#include <thread>void increment(int& x) { ++x;}int main() { int num = 5; std::thread t(increment, std::ref(num)); t.join(); std::cout << "num = " << num << "\n"; // 输出:num = 6 return 0;}当多个线程访问共享资源时,必须使用同步机制防止数据竞争。C++ 提供了 std::mutex:
#include <iostream>#include <thread>#include <mutex>std::mutex mtx;int counter = 0;void increase_counter() { for (int i = 0; i < 1000; ++i) { mtx.lock(); ++counter; mtx.unlock(); }}int main() { std::thread t1(increase_counter); std::thread t2(increase_counter); t1.join(); t2.join(); std::cout << "Final counter = " << counter << "\n"; return 0;}更安全的做法是使用 RAII 风格的 std::lock_guard,它会在作用域结束时自动解锁:
void increase_counter() { for (int i = 0; i < 1000; ++i) { std::lock_guard<std::mutex> lock(mtx); ++counter; // 自动解锁 }}std::thread 对象销毁前调用 join() 或 detach()。std::lock_guard 或 std::unique_lock 管理互斥锁,避免死锁。std::async、std::future 等。通过本教程,你已经掌握了 C++ thread库 的基本用法,包括线程创建、参数传递、线程管理以及同步机制。这些知识是构建高效、安全的 C++并发 程序的基础。随着实践的深入,你还可以探索条件变量、原子操作、线程池等高级主题。
记住:多线程虽强大,但也复杂。务必遵循最佳实践,避免常见的并发陷阱。
关键词回顾:C++ thread库、多线程编程、C++并发、thread使用教程。
本文由主机测评网于2025-12-04发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/2025122615.html