Opened 8 years ago

Last modified 5 months ago

#791 new discussion

lock ostreams

Reported by: Peter Owned by: Peter
Priority: minor Milestone: yat 0.x+
Component: utility Version:
Keywords: Cc:


I have a program in which the different threads send some output to std::clog and quite often the output is intermingled since an output typically is generated via multiple calls to operator<<.

Would be nice to have an easy way to lock access to ostreams in particular the global instances std::cout and std::clog.

Perhaps an interface could look something like this:

LockMe(std::clog) << "Hey my name is " << who_am_I << "\n";

where the LockMe is a class constructor and std::clog is the lock is released in destructor of same class, i.e., after "\n" has been passed to std::clog.

Change History (2)

comment:1 Changed 5 years ago by Peter

There's a proposal for C++20 on synchronized ostreams:

comment:2 Changed 5 months ago by Peter

As suggested above, this functionality was included in C++20.

Note: See TracTickets for help on using tickets.