测试代码thread.cpp

#include <thread>
#include <iostream>
using namespace std;void run(int n){for(int i = 0; i < 5; i++) {cout << "thread " << n << endl;}
}
int main() {cout << "hahahha" << endl;thread t1(run, 1);thread t2(run, 2);t1.join();t2.join();return 0;
}

直接g++编译一下
g++ thread.cpp -o mythread
会发现报错

In file included from /usr/include/c++/5/thread:35:0,from thread.cpp:1:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.#error This file requires compiler and library support \

解决方法是g++参数添加“-std=c++11”,但接着编译时,又会报另一个问题

$ g++ thread.cpp -std=c++11 -o mythread
/tmp/ccbLvKA8.o: In function `std::thread::thread<void (&)(int), int>(void (&)(int), int&&)':
thread.cpp:(.text._ZNSt6threadC2IRFviEJiEEEOT_DpOT0_[_ZNSt6threadC5IRFviEJiEEEOT_DpOT0_]+0x93): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status

解决办法是编译参数添加“-lpthread”

$ g++ thread.cpp -std=c++11 -o mythread -lpthead
/usr/bin/ld: cannot find -lpthead
collect2: error: ld returned 1 exit status
$ g++ thread.cpp -std=c++11 -o mythread -lpthread
$ ./mythread
hahahha
thread1
thread1
threadthread2
thread2
thread2
thread2
thread2
1
thread1
thread1

因为cout打印不是原子操作,会导致thread1和thread2混着打印
先试着使用mutex加锁,更改一下代码

#include <thread>
#include <iostream>
#include <mutex>using namespace std;mutex mLock;void run(int n){for(int i = 0; i < 5; i++) {mLock.lock();cout << "thread" << n << endl;mLock.unlock();}
}int main() {cout << "hahahha" << endl;thread t1(run, 1);thread t2(run, 2);t1.join();t2.join();return 0;
}

运行结果:
$ ./mythread
hahahha
thread1
thread1
thread1
thread1
thread1
thread2
thread2
thread2
thread2
thread2

因为在run函数中有可能会发现异常,导致unlock没有执行到,进程可能会挂死
解决办法是使用lock_guard,由lock_guard控制构造和析构流程,会自动unlock,运行结果是一样的。

void run(int n){for(int i = 0; i < 5; i++) {//mLock.lock();lock_guard <mutex> guard(mLock);cout << "thread" << n << endl;//mLock.unlock();}
}

也可以利用unique_lock,这个功能和lock_guard类似,但提供的功能更多些,try_to_lock尝试去lock,可通过owns_lock判断是否持锁成功

void run(int n){for(int i = 0; i < 5; i++) {//mLock.lock();//lock_guard <mutex> guard(mLock);unique_lock <mutex> lock(mLock, try_to_lock);//try to lockcout << "thread" << n << ", lock result:" << lock.owns_lock() << endl;//mLock.unlock();}
}

运行结果,偶尔能看到没有持到锁的情况。
$ ./mythread
hahahha
thread1, lock result:1
thread1, lock result:1
thread1, lock result:1
thread1, lock result:1
thread1, lock result:1
thread2, lock result:0
thread2, lock result:1
thread2, lock result:1
thread2, lock result:1
thread2, lock result:1

g++编译c++11 thread报错问题 及c++多线程操作相关推荐

  1. 重新编译CDH版本hadoop报错:Non-resolvable parent POM: Could not transfer artifact com.

    重新编译CDH版本hadoop报错: Could not transfer artifact com.cloudera.cdh:cdh-root:pom:5.14.0 from/to cdh.repo ...

  2. 【错误记录】记录 Android 命令行执行 Java 程序中出现的错误 ( dx 打包 PC 可执行文件报错 | dalvik 命令执行 kotlin 编译的 dex 文件报错 )

    文章目录 前言 一.Android 命令行与 PC 可执行 JAR 文件不兼容 二.Android 命令行使用 dalvik 命令不能直接执行 Kotlin 编译的 dex 文件 前言 尝试在 And ...

  3. 编译内核 make modules_install报错make[1]: *** [arch/x86/crypto/aegis128-aesni.ko] Error 1 Makefile:1281: r

    Linux编译内核 make modules_install报错make[1]: *** [arch/x86/crypto/aegis128-aesni.ko] Error 1 Makefile:12 ...

  4. 编译PX4时,报错error ‘i‘ does not name a type __ULong i[2];解决方法

    编译PX4时,报错error: 'i' does not name a type __ULong i[2];解决方法 在编译PX4的时候,会遇到报错: /usr/include/newlib/math ...

  5. php5.4与php5.2,升级php 5.2.14 到5.4.11版本报错问题

    升级php 5.2.14 到5.4.11版本报错问题 发布时间:2020-03-07 21:32:31 来源:51CTO 阅读:621 作者:djpeters 升级php 5.2.14 到5.4.11 ...

  6. 关于 国产麒麟系统编译Qt项目是报错:error: cannot find -lGL 的解决方法

    若该文为原创文章,转载请注明原文出处 本文章博客地址:https://hpzwl.blog.csdn.net/article/details/123784051 红胖子(红模仿)的博文大全:开发技术集 ...

  7. java使用cmd编译中文时错误,解决Notepad++编写的Java程序在cmd窗口编译时中文注释报错问题...

    解决Notepad++编写的Java程序在cmd窗口编译时中文注释报错问题 解决Notepad++编写的Java程序在cmd窗口编译时中文注释报错问题 在刚开始学习Java的过程中,考虑到记事本应用没 ...

  8. ubuntu20.04静态编译qt5.14.2报错

    ubuntu20.04静态编译qt5.14.2报错 collect2: error: ld returned 1 exit status make[3]: *** [Makefile:134: -/- ...

  9. vs code使用Easy Sass插件编译sass文件路径报错问题解决

    问题: scss文件中使用了@import来引入另一个scss文件,两个scss文件不在同一层级,所以我使用相对路径的写法来引用文件,但是使用vs code里面安装的扩展Easy Sass进行编译时, ...

最新文章

  1. 杭州/北京内推 | 阿里达摩院自然语言智能生物医学团队招聘研究型实习生
  2. Java多线程技术-Volatile关键字解析
  3. DAY77-Django框架(八)
  4. 前端学习(505):垂直居中的第一种方式的优点和缺点
  5. 【剑指offer】面试题10- I:斐波那契数列(Java)
  6. 每个开发人员都需要学Python?看看大佬是怎么说的!
  7. HTML之图片标签、音视频标签
  8. mysql inno_mysql inno优化配置方法
  9. Linux的10个经典彩蛋
  10. AcWing Django框架课第一节笔记
  11. java APP支付宝支付
  12. oracle中alter用法,Oraclealter用法
  13. Gitlab用户角色权限Guest、Reporter、Developer、Master、Owner
  14. 基于opencv的图像拼接
  15. 泰安中专学校计算机专业,泰安市岱岳区职业中等专业学校
  16. 人工神经网络的算法原理,深度神经网络算法原理
  17. 微软预览word_如何在Microsoft Word中更改语言
  18. 【最全】Spring Boot 实现分布式锁——这才是实现分布式锁的正确姿势!
  19. BCB vs. VC++
  20. 不叹惜、不呼唤我也不哭泣

热门文章

  1. ADO编程中ATL所遇到的定义问题
  2. Windows 2000/XP中对窗口进行透明化
  3. VC下设置Excel单元格的边框
  4. 经典面试题(52):以下代码将输出的结果是什么?
  5. c语言 swap交换函数_重审C中老生常谈的swap函数交换数值
  6. 吴恩达机器学习笔记十三之推荐系统
  7. R沟通|Bookdown中文书稿写作手册(中)
  8. 这个处理不同基因组区域关系的工具集很不错!
  9. 分子进化和系统发育的基础知识
  10. NGS系列文章 - 高通量测序原理