码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 创建10个线程并发执行(STL/Windows/Linux)


    C++并发编程入门 目录

    STL 写法

    1. #include
    2. #include
    3. using namespace std;
    4. void thread_fun(int arg)
    5. {
    6. cout << "one STL thread " << arg << " !" << endl;
    7. }
    8. int main(void)
    9. {
    10. int thread_count = 10;
    11. int id_array[10] = { 1,2,3,4,5,6,7,8,9,10 };
    12. std::thread thread_arr[10] = { };
    13. for (int i = 0; i < thread_count; i++)
    14. {
    15. thread_arr[i] = std::thread(thread_fun, id_array[i]);
    16. }
    17. for (int i = 0; i < thread_count; i++)
    18. {
    19. thread_arr[i].join();
    20. }
    21. return 0;
    22. }

    Windows 写法

    1. #include
    2. #include
    3. using namespace std;
    4. DWORD WINAPI ThreadFun(LPVOID lpParamter)
    5. {
    6. //转成整数地址,再对地址解引用取出整数
    7. cout << "one Windows thread "<< *(int*)lpParamter<<" !" << endl;
    8. return 0;
    9. }
    10. int main()
    11. {
    12. int thread_count = 10;
    13. HANDLE handleArr[10] = {NULL};
    14. //给线程函数传递的数据,用来标记线程
    15. int data[10] = { 1,2,3,4,5,6,7,8,9,10 };
    16. for (int i = 0; i < 10; i++)
    17. {
    18. handleArr[i] = CreateThread(NULL, 0, ThreadFun, &data[i], 0, NULL);
    19. }
    20. //等待10个线程结束
    21. WaitForMultipleObjects(thread_count, handleArr, TRUE, INFINITE);
    22. for (int i = 0; i < thread_count; i++)
    23. {
    24. CloseHandle(handleArr[i]);
    25. }
    26. return 0;
    27. }

    一次执行结果


     

    另一次执行结果

    Linux 写法

    1. #include
    2. #include
    3. using namespace std;
    4. void* thread_fun(void *arg)
    5. {
    6. cout << "one Linux thread "<< *(int*)arg<<" !" << endl;
    7. return 0;
    8. }
    9. int main(void)
    10. {
    11. int thread_count = 10;
    12. int id_array[10] = { 1,2,3,4,5,6,7,8,9,10 };
    13. pthread_t thread_arr[10] = { 0 };
    14. for (int i = 0; i < thread_count; ++i)
    15. {
    16. pthread_create(&thread_arr[i], NULL, thread_fun, &id_array[i]);
    17. }
    18. //让线程运行直到结束
    19. for (int i = 0; i < thread_count; i++)
    20. {
    21. pthread_join(thread_arr[i], NULL);
    22. }
    23. return 0;
    24. }

    一次执行结果

    另一次的执行结果

    再一次执行结果

  • 相关阅读:
    根据递归原理设计一个简单的代码生成器
    Apache Commons Bridge For Scala
    运行报错(三)git bash报错fatal: detected dubious ownership in repository at
    26栈和队列-简单实践
    vue3的ref全家桶---kalrry---ing
    Mybatis-Plus的一些优雅用法
    Android 11.0 mt6771新增分区功能实现一
    MuleSoft Certified Integration Architect - Level 1 Practice Exam
    将符号分隔的文本文件txt转换为excel的实现
    【Markdown小技巧】 整理小图标和表情符号
  • 原文地址:https://blog.csdn.net/ClamReason/article/details/132647250
  • 最新文章
  • 和AI一起搞事情#5:技能进阶与Claude Design初体验
    .NET 11 Preview 4 正式发布:Runtime-Async 全面启用、Process API 大幅扩展
    大数据没那么远:把散乱数据理顺,让业务敢用
    高性能百度OCR ONNX Runtime C#实现
    C#/.NET/.NET Core技术前沿周刊 | 第 70 期(2026年5.01-5.10)
    Triton学习 · Part 1 · Hello, world!
    manyspeech-cli 本地命令行语音识别工具
    PortSwigger SQL注入LAB5 & LAB6
    Go-Spring 项目概览
    "HyFormer: Revisiting the Roles of Sequence Modeling and Feature Interaction in CTR Prediction" 论文笔记
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号