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
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论
52