Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
c++如何实现多线程编程_c++ std::thread使用方法_创想鸟

c++如何实现多线程编程_c++ std::thread使用方法

C++多线程编程通过std::thread实现,支持函数、Lambda创建线程,可传值或引用参数,需调用join()或detach()管理线程生命周期,并可使用hardware_concurrency()获取硬件并发数。

c++如何实现多线程编程_c++ std::thread使用方法

C++ 实现多线程编程主要通过标准库中的 std::thread 来完成。从 C++11 开始,C++ 标准提供了对多线程的原生支持,使得开发者无需依赖第三方库(如 pthread)也能轻松编写跨平台的多线程程序。

创建和启动线程

使用 std::thread 可以通过函数、函数对象、Lambda 表达式等方式创建线程。

基本语法:

包含头文件:#include 构造一个 thread 对象并传入可调用目标示例1:使用普通函数

#include #include void say_hello() {    std::cout << "Hello from thread!" << std::endl;}int main() {    std::thread t(say_hello);  // 启动新线程执行 say_hello    t.join();  // 等待线程结束    return 0;}

示例2:使用 Lambda 表达式

#include #include int main() {    std::thread t([](){        std::cout << "Lambda in thread" << std::endl;    });    t.join();    return 0;}

传递参数给线程函数

向线程函数传递参数时,需在构造 thread 对象时一并传入。注意:默认是值传递,若要引用传递,必须使用 std::ref

立即学习“C++免费学习笔记(深入)”;

示例:传参与引用传递

#include #include void print_num(int n, std::string& msg) {    n += 10;    msg += " (modified)";    std::cout << "n = " << n << ", msg = " << msg << std::endl;}int main() {    int val = 5;    std::string str = "Hello";    std::thread t(print_num, val, std::ref(str));  // 引用传递 str    t.join();    std::cout << "After thread: val = " << val << ", str = " << str << std::endl;    return 0;}

输出中可以看到,val 的修改在线程内无效(值传递),而 str 被修改了(引用传递)。

线程的等待与分离

每个 thread 对象有两种运行模式:

join():主线程等待该线程执行完毕 detach():线程与主线程分离,独立运行(后台运行)

必须在 thread 对象销毁前调用其中之一,否则程序会终止(调用 std::terminate)。

示例:分离线程

#include #include #include void background_task() {    std::this_thread::sleep_for(std::chrono::seconds(2));    std::cout << "Background task done." << std::endl;}int main() {    std::thread t(background_task);    t.detach();  // 分离线程    std::cout << "Main thread continues..." << std::endl;    std::this_thread::sleep_for(std::chrono::seconds(3));  // 确保主线程不立即退出    return 0;}

获取硬件支持的并发线程数

可以使用 std::thread::hardware_concurrency() 查询系统建议的最大并发线程数。

unsigned int max_threads = std::thread::hardware_concurrency();std::cout << "Supports " << max_threads << " concurrent threads.n";

这个值只是一个提示,可能返回 0(表示无法确定)。

基本上就这些。掌握 std::thread 的创建、参数传递、生命周期管理,就能应对大多数基础多线程场景。实际开发中常配合 mutex、条件变量等同步机制使用,避免数据竞争。

以上就是c++++如何实现多线程编程_c++ std::thread使用方法的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1488270.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
C++右值引用与移动语义_C++11 std::move提升性能的原理
上一篇 2025年12月19日 11:24:59
C++的atomic是什么_C++11使用std::atomic实现无锁编程的基础
下一篇 2025年12月19日 11:25:10

相关推荐

发表回复

登录后才能评论
关注微信