• 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 - 博客园

  • 相关阅读:
    【echarts】07、echarts+vue2 - 环形图
    保姆级服务器安装宝塔面板教程
    mac安装+配置python3环境
    java毕业设计城市垃圾分类管理系统mybatis+源码+调试部署+系统+数据库+lw
    POI导出Excel设置背景颜色不生效
    Redis之介绍、下载安装
    Solidity中的变量种类以及常见的全局变量
    手写ioc
    【Godot4自学手册】第三十九节利用shader(着色器)给游戏添加一层雾气效果
    类与对象(中篇)
  • 原文地址:https://blog.csdn.net/qq_41722795/article/details/126123979