• C++ 多线程(包含linux) cmake


    Linux 多线程的使用

     

    工具: clion、cmake

    平台:Ubuntu

    在使用 多线程时出现以下错误:

    /usr/include/c++/9/thread:126: undefined reference to `pthread_create'

    解决方案:在camkelist 文件中设置 g++

    set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++11 -pthread")

    多线程

    代码:

    1. //
    2. // Created by ly on 2022/8/2.
    3. //
    4. #include
    5. #include
    6. #include
    7. void test1(){
    8. std::cout << "child thread test" << std::endl;
    9. }
    10. void test2(){
    11. std::thread t(test1);
    12. t.join();
    13. }
    14. void *test4(void* args){
    15. std::cout<< "child pthread test" << std::endl;
    16. return 0;
    17. }
    18. // 线程的运行函数
    19. void* say_hello(void* args)
    20. {
    21. std::cout << "Hello Runoob!" << std::endl;
    22. return 0;
    23. }
    24. void test3(){
    25. //定义线程的ID变量,多个变量使用 数组
    26. pthread_t tId;
    27. //参数依次是:创建的线程id,线程参数,调用的函数,传入的函数参数
    28. //int ret = pthread_create(&tId, NULL, reinterpret_cast(test1), NULL);
    29. int ret = pthread_create(&tId, NULL, test4, NULL);
    30. pthread_exit(NULL);
    31. }
    32. int main(){
    33. test2();
    34. std::cout << "hello world" << std::endl;
    35. return 1;
    36. }

     cmakelist.txt:

    1. cmake_minimum_required(VERSION 3.17)
    2. project(MultipleThreadTest)
    3. set(CMAKE_CXX_STANDARD 11)
    4. set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++11 -pthread")
    5. add_executable(MultipleThreadTest main.cpp main.cpp)

    C++ 11 Thread

     使用标准库 下的多线程

    1. //
    2. // Created by ly on 2022/8/2.
    3. //
    4. #include
    5. #include
    6. void test1(){
    7. std::cout << "child thread test" << std::endl;
    8. }
    9. void test2(){
    10. std::thread t(test1);
    11. t.join();
    12. }
    13. int main(){
    14. test2();
    15. std::cout << "hello world" << std::endl;
    16. return 1;
    17. }

    运行结果:

    POSIX Threads

     使用 POSIX 编写多线程 C++ 程序

    1. //
    2. // Created by ly on 2022/8/2.
    3. //
    4. #include
    5. #include
    6. void *test4(void* args){
    7. std::cout<< "child pthread test" << std::endl;
    8. return 0;
    9. }
    10. // 线程的运行函数
    11. void* say_hello(void* args)
    12. {
    13. std::cout << "Hello Runoob!" << std::endl;
    14. return 0;
    15. }
    16. void test3(){
    17. //定义线程的ID变量,多个变量使用 数组
    18. pthread_t tId;
    19. //参数依次是:创建的线程id,线程参数,调用的函数,传入的函数参数
    20. //int ret = pthread_create(&tId, NULL, reinterpret_cast(test1), NULL);
    21. int ret = pthread_create(&tId, NULL, test4, NULL);
    22. pthread_exit(NULL);
    23. }
    24. int main(){
    25. test3();
    26. std::cout << "hello world" << std::endl;
    27. return 1;
    28. }

    运行结果:

    reinterpret_cast 类型转换

    如果定义的函数不一致 ,C++提供了reinterpret_cast用于任意类型的转换。

    语法:reinpreter_cast (exp)

    其中, reinterpret_cast后的尖括号中的type-id类型必须是一个指针、引用、算术类型、函数指针或者成员指针。它可以把一个指针转换成一个整数,也可以把一个整数转换成一个指针。

    例如:

    reinterpret_cast(test1)

    参考文档:c++学习笔记(四)- 多线程 互斥 cmake - tszs_song - 博客园

  • 相关阅读:
    基于线性卡尔曼滤波器和粒子滤波器估计地形高度(Matlab代码实现)
    计算机毕业设计Java网上主题超市系统(源码+系统+mysql数据库+Lw文档)
    【树莓派开发日记1】1.3k预算的树莓派+显示屏+键鼠的选型与拆箱物理安装
    java-抽象类、抽象方法
    中小制造企业为什么要做MES智能化升级?看了本文你就知道了
    【Vue面试题五】说说你对Vue生命周期的理解?
    java进阶学习路线
    天选之子Linux是如何发展起来的?为何对全球IT行业的影响如此之大?
    react中JSX的详解
    (一)JPA的快速入门
  • 原文地址:https://blog.csdn.net/qq_41722795/article/details/126123979