码农知识堂 - 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. }

    一次执行结果

    另一次的执行结果

    再一次执行结果

  • 相关阅读:
    Matplotlib面向对象接口
    COVID疫苗加强针来袭,是否该接种?
    财务管理-报支单提交中和付款申请被驳回解决措施
    Redis 7.0 新功能新特性总览
    商店销售预测(回归&随机森林)
    车载u盘支持什么格式音乐?怎么把音乐转成MP3格式?
    【论文阅读】A Survey on Video Diffusion Models
    debian 已安装命令找不到 解决方法
    npm、nrm两种方式查看源和切换镜像
    Vue学习:vue生命周期
  • 原文地址:https://blog.csdn.net/ClamReason/article/details/132647250
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号