1 线程安全的std::cout
最近在多个子线程中使用std::cout输出日志信息发现std::cout不是线程安全的,无法保持线程同步,导致日志信息无法按照固定顺序输出,现象如下:
所以对std::cout做了一个封装以保证多线程之间的同步,代码如下:
#include <iostream>
#include <string>
#include <mutex>
namespace ThreadSafePrint
{
static std::mutex m_CoutMutex;
struct cout
{
std::unique_lock<std::mutex> m_Lock;
cout():
m_Lock(std::unique_lock<std::mutex>(m_CoutMutex))
{
}
template<typename T>
cout& operator<<(const T& message)
{
std::cout << message;
return *this;
}
cout& operator<<(std::ostream& (*fp)(std::ostream&))
{
std::cout << fp;
return *this;
}
};
}
调用示例:
ThreadSafePrint::cout() << "Hello World" << std::endl;
测试程序如下:
#include <iostream>
#include <string>
#include <mutex>
#include <thread>
namespace ThreadSafePrint
{
static std::mutex m_CoutMutex;
struct cout
{
std::unique_lock<std::mutex> m_Lock;
cout():
m_Lock(std::unique_lock<std::mutex>(m_CoutMutex))
{
}
template<typename T>
cout& operator<<(const T& message)
{
std::cout << message;
return *this;
}
cout& operator<<(std::ostream& (*fp)(std::ostream&))
{
std::cout << fp;
return *this;
}
};
}
void Thread1()
{
for (int i=0;i<10000;++i)
{
ThreadSafePrint::cout() <<std::this_thread::get_id() <<" : aa" << std::endl;
//std::cout << std::this_thread::get_id() << " : aa" << std::endl;
}
}
void Thread2()
{
for (int i = 0; i < 10000; ++i)
{
ThreadSafePrint::cout() << std::this_thread::get_id() << " : bb" << std::endl;
//std::cout << std::this_thread::get_id() << " : bb" << std::endl;
}
}
void Thread3()
{
for (int i = 0; i < 10000; ++i)
{
ThreadSafePrint::cout() << std::this_thread::get_id() << " : cc" << std::endl;
//std::cout << std::this_thread::get_id() << " : cc" << std::endl;
}
}
int main()
{
std::thread printThread1(Thread1);
std::thread printThread2(Thread2);
std::thread printThread3(Thread3);
printThread1.join();
printThread2.join();
printThread3.join();
getchar();
return 0;
}
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – 线程安全的std::cout
原文链接:https://www.stubbornhuang.com/1129/
发布于:2021年02月01日 11:35:05
修改于:2023年06月26日 21:57:07
当前分类随机文章推荐
- C++11 - 使用std::thread在类内部以成员函数作为多线程函数执行异步操作 阅读3213次,点赞0次
- C++ - 控制台程序在控制台窗口可变参数格式化带颜色输出日志信息 阅读3673次,点赞0次
- C++STL容器 – std::vector容器修改、元素操作总结 push_back,emplace_back,emplace,insert,erase,pop_back,clear,resize,swap 阅读1312次,点赞1次
- C++ - 使用Spout2将视频流发送到OBS 阅读233次,点赞0次
- C++ - int转string方法总结 阅读6996次,点赞0次
- C++ - 获取当前进程内存使用情况 阅读10663次,点赞10次
- C++ - 常用的C++命令行参数解析第三方库 阅读4348次,点赞3次
- C++ - 使用std::chrono获取当前秒级/毫秒级/微秒级/纳秒级时间戳 阅读5175次,点赞0次
- C++ - 使用Crypto++/CryptoPP加解密库对字符串或者文件进行AES256加密 阅读4630次,点赞1次
- OpenCV | OpenGL - OpenCV的cv::mat转换为OpenGL的GL_TEXTURE_2D纹理数据 阅读550次,点赞0次
全站随机文章推荐
- Onnx - onnx模型简化与优化 阅读725次,点赞0次
- 资源分享 - Physically Based Rendering From Theory To Implementation (Fourth Edition)英文高清PDF下载 阅读1096次,点赞0次
- 资源分享 - Mathematics for Computer Graphics , Third Edition 英文高清PDF下载 阅读1606次,点赞0次
- WordPress - 切换标签离开页面时修改网站title对用户进行提示 阅读230次,点赞0次
- human3.6m : Download(数据集下载) 阅读28809次,点赞40次
- C++ - 拷贝构造函数与拷贝构造函数调用时机 阅读567次,点赞0次
- 资源分享 - Jim Blinn's Corner - Notation, Notation, Notation 英文高清PDF下载 阅读2375次,点赞0次
- 深度学习 - 以一个极简单的中英文翻译Demo彻底理解Transformer 阅读1117次,点赞0次
- 资源分享 - GLSL Essentials - Enrich your 3D scenes with the power of GLSL 英文高清PDF下载 阅读2578次,点赞0次
- FFmpeg - RGB图像编码为h264出现垂直旋转的问题 阅读4020次,点赞0次
评论
169