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


    C++并发编程入门 目录

    STL 写法

    1. #include
    2. #include
    3. using namespace std;
    4. void thread_fun1(void)
    5. {
    6. cout << "one STL thread 1!" << endl;
    7. }
    8. void thread_fun2(void)
    9. {
    10. cout << "one STL thread 2!" << endl;
    11. }
    12. int main(void)
    13. {
    14. std::thread thread1(thread_fun1);
    15. std::thread thread2(thread_fun2);
    16. thread1.join();
    17. thread2.join();
    18. return 0;
    19. }

    Windows 写法

    1. #include
    2. #include
    3. using namespace std;
    4. DWORD WINAPI ThreadFun1(LPVOID lpParamter)
    5. {
    6. cout << "one Windows thread 1!" << endl;
    7. return 0;
    8. }
    9. DWORD WINAPI ThreadFun2(LPVOID lpParamter)
    10. {
    11. cout << "one Windows thread 2!" << endl;
    12. return 0;
    13. }
    14. int main()
    15. {
    16. HANDLE hThread1 = CreateThread(NULL, 0, ThreadFun1, NULL, 0, NULL);
    17. HANDLE hThread2 = CreateThread(NULL, 0, ThreadFun2, NULL, 0, NULL);
    18. HANDLE handleArr[] = { hThread1 , hThread2 };
    19. //等待两个线程结束
    20. WaitForMultipleObjects(2, handleArr, TRUE, INFINITE);
    21. CloseHandle(hThread1);
    22. CloseHandle(hThread2);
    23. return 0;
    24. }

    三次执行结果

    每个线程都打印一句话

    第一个线程和第二个线程交叉占用控制台

    居然是先执行的第二个线程

    Linux 写法

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

    一次执行结果

    另一次执行结果

  • 相关阅读:
    软件测试面试技巧有哪些?这几点你得知道,不然后悔都来不及
    Android OpenGL ES 3.0 粒子特效
    connection is being used##server is in use and cannot be deleted
    【IoT NTN】3GPP R18中关于各类IoT设备在NTN中的增强和扩展
    使用AI编写测试用例——详细教程
    HotSpot JVM 中的应用程序/动态类数据共享
    Vue3使用Vite创建项目
    Django ORM 多表操作:一对一、一对多、多对多的增删改,基于对象/双下划线的跨表查询
    java读取指定文件夹下的全部文件,并输出文件名,文件大小,文件创建时间
    Deformable ConvNets v2: More Deformable, Better Results 第二代可变形卷积论文精读与解析
  • 原文地址:https://blog.csdn.net/ClamReason/article/details/132647223
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号