Libftpp
A modern C++ library
ThreadSafeIOStream Class Reference

Thread-Safe I/O Stream. More...

#include <thread_safe_iostream.hpp>

Public Member Functions

template<typename T >
ThreadSafeIOStreamoperator>> (T &value)
 
ThreadSafeIOStreamoperator<< (std::ostream &(*funct)(std::ostream &))
 
template<typename T >
ThreadSafeIOStreamoperator<< (const T &value)
 
void setPrefix (const std::string &prefix)
 
template<typename T >
void prompt (const std::string &question, T &dest)
 

Detailed Description

Thread-Safe I/O Stream.

This class provides a thread-safe wrapper around standard input and output streams. It ensures that multiple threads can safely read from and write to the console without interleaving their output. It also supports setting a prefix for each output line to identify the source thread.

Note
Uses a static mutex to synchronize access to std::cout and std::cin
Supports stream operators (<<, >>) for easy usage
Allows setting a prefix for output lines
include "thread_safe_iostream.hpp"
threadSafeCout.setPrefix("[Thread 1] ");
threadSafeCout << "Hello from Thread 1!" << std::endl;
void setPrefix(const std::string &prefix)
thread_local ThreadSafeIOStream threadSafeCout

Definition at line 30 of file thread_safe_iostream.hpp.

Member Function Documentation

◆ operator<<() [1/2]

template<typename T >
ThreadSafeIOStream& ThreadSafeIOStream::operator<< ( const T &  value)
inline

Definition at line 63 of file thread_safe_iostream.hpp.

◆ operator<<() [2/2]

ThreadSafeIOStream & ThreadSafeIOStream::operator<< ( std::ostream &(*)(std::ostream &)  funct)

Definition at line 12 of file thread_safe_iostream.cpp.

◆ operator>>()

template<typename T >
ThreadSafeIOStream& ThreadSafeIOStream::operator>> ( T &  value)
inline

Definition at line 40 of file thread_safe_iostream.hpp.

◆ prompt()

template<typename T >
void ThreadSafeIOStream::prompt ( const std::string &  question,
T &  dest 
)
inline

Definition at line 92 of file thread_safe_iostream.hpp.

◆ setPrefix()

void ThreadSafeIOStream::setPrefix ( const std::string &  prefix)

Definition at line 6 of file thread_safe_iostream.cpp.

Referenced by Thread::Thread().