OMUTEXSTREAM(3bobcat)
π FBB::OMutexStream π
okay bestie, gather round, because we're talking about FBB::OMutexStream from the 3bobcat corner of the manual-verse, and it's giving "mutex protected std::ostream" energy, which β ngl β is exactly what it is. β¨
πΈ NAME πΈ
FBB::OMutexStream β Offers mutex protected std::ostream facilities π«Ά
β¨ SYNOPSIS β¨
#include <bobcat/omutexstream>
Linking option: -lbobcat
π DESCRIPTION π
okay so picture this: you're in a multi-threaded program, right? Multiple threads are all writing to the same output stream, and because context switches are a thing, the output gets all intermixed and messy. π The way you prevent that is with mutexes: before writing to the output stream, you acquire a mutex lock, and you release the lock once the output is done. periodt.
OMutexStream supports writing to output streams while handling those mutex locks for you. The OMutexStream object itself is initialized with the std::ostream object that receives the information written to the OMutexStream object. This object, like std::cout and std::err usually is, is typically defined as a globally accessible object. π
When you insert information into the OMutexStream object, OMutexStream first returns an OMutexStream::Out object, whose constructor locks the OMutexStream::Out mutex. The OMutexStream::Out object's lifetime ends at the end of the insertion statement, and at that time its destructor releases the lock. iconic behaviour, truly.
In many cases this is appropriate. Especially when statements end in newlines ('\n' or endl) this results in clean output. π But in some cases this approach doesn't work. Consider the situation where the output is produced by a series of iterations in a for-statement. For these cases OMutexStream offers the member ostream returning an OMutexStream::Out object. As that object also locks the mutex, the lock also remains active during its lifetime. During its lifetime separate OMutexStream insertions expressions may be executed. E.g., the following code fragment will complete all output forcing competing threads to wait:
void fun()
{
OMutexStream mout{ std::cout }; // create an OMutexStream object
{
auto out{ mout.ostream() } // locks the mutex (lock #1)
mout << "Hello "; // also locks (lock #2, at ;
// back to lock #1)
out << "world\n";
} // βoutβ releases the lock
}
β οΈ bestie, be careful to restrict the lifetime of the object returned by OMutexStream::ostream(). do not let it hang around being all main character for longer than it needs to. π«£
π NAMESPACE π
FBB
All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB. no cap. π§
π INHERITS FROM π
(via OMutexStream::Out) std::ostream
π CONSTRUCTORS π
OMutexStream(std::ostream &out)β TheOMutexStreamobject is initialized with anstd::ostreamobject.
Copy and move constructors (and assignment operators) are available. slay. β¨
π₯ OVERLOADED OPERATORS π₯
OMutexStream::Out operator<<(OMutexStream const &mstr, Tp &&tp)β This member is a function template. Its forwarding reference is passed on to theOMutexStream::Outobject constructed bymstr. SinceOMutexStream::Outconstructors lock the class's mutex, and sinceOMutexStream::Outwas derived fromstd::ostreamall insertion and other operations that are allowed forstd::ostreamcan also be used forOMutexStream::Outobjects. it's giving everything std::ostream does, but with a lock. πOMutexStream::Out operator<<(OMutexStream const &mstr, Ret &(*manip)(Ret &os))β This member also is a function template. It is used for inserting manipulators without arguments intoOMutexStream::Outobjects.
β MEMBER FUNCTIONS β
OMutexStream::Out ostream() constβ AOMutexStream::Outobject is returned that has locked its mutex, and will keep the lock during its lifetime. All functionality of thestd::ostreamclass can also be used for theOMutexStream::Outobject returned byostream.
Be careful to restrict the lifetime of the object returned by OMutexStream::ostream() to avoid needlessly long mutex locks. π she will hold the lock forever if you let her, and nobody wants that.
π EXAMPLE π
#include <iostream>
#include <thread>
#include <chrono>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <bobcat/omutexstream>
using namespace std;
using namespace FBB;
OMutexStream mout(cout);
void run(int nr)
{
for (size_t idx = 0; idx != 3; ++idx)
{
mout << "hello world 1 from thread " << nr << ": " <<
log(rand()) << endl;
this_thread::sleep_for(
chrono::milliseconds(200 + rand() % 800));
mout << "hello world 2 from thread " << nr << ": " <<
log(rand()) << β\nβ;
this_thread::sleep_for(
chrono::milliseconds(200 + rand() % 800));
}
auto out{ mout.ostream() };
cout << nr << ": " << out.tellp() << β\nβ;
}
int main()
{
srand(time(0));
thread t1(run, 1);
thread t2(run, 2);
thread t3(run, 3);
thread t4(run, 4);
t1.join();
t2.join();
t3.join();
t4.join();
}
π¦ FILES π¦
bobcat/omutexstream β defines the class interface
π SEE ALSO π
π BUGS π
None Reported. (we love a clean bug list, bestie π«Ά)
πΈ BOBCAT PROJECT FILES πΈ
https://fbb-git.gitlab.io/bobcat/: gitlab project page;
Debian Bobcat project files:
libbobcat6: debian package containing the shared library, changelog and copyright note;libbobcat-dev: debian package containing the static library, headers, manual pages, and developer info;
π BOBCAT π
Bobcat is an acronym of `Brokkenβs Own Base Classes And Templatesβ. π
β COPYRIGHT β
This is free software, distributed under the terms of the GNU General Public License (GPL).
π AUTHOR π
Frank B. Brokken (f.b.brokken@rug.nl). absolute icon, no notes. π
okay and that's a wrap, bestie! π this lil slice of the FBB::O...xStream(3bobcat) manual ships as part of libbobcat-dev_6.15.01, dated 2005-2026. go forth and lock your streams responsibly. periodt. β¨ππ¦
π¦ where this page comes from
- ubuntu stonking: libbobcat-dev 6.15.01-1 Β· GPL-3.0-or-later WITH OpenSSL exception
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms