I'm currently writing a wrapper for STL stream to synchronize write calls from multiple threads. I have the following (simplified) code:
class Synchronize {
private:
std::stringstream ss;
public:
void write(std::string& str) {
// locking ...
ss << str;
// unlocking ...
};
// other stuff ..
};
Synchronize& operator<<(Synchronize& o, std::string& str) {
o.write(str);
return o;
}
Synchronize& operator<<(Synchronize* o, std::string& str) {
o->write(str);
return *o;
}
Its now possible to call the write()
method by using the <<
operator on an object of the Synchronize
class, but only by using a std::string
. And std::stringstream
also takes a lot of other stuff like int
s and float
s.
Is it possible to add this functionality to my Synchronize
class without a ton of own operator<<
functions? Would templates help? Or should I extend some class from the iostream
library?
You can turn your operator overload into a friend template
Inside your class write
Then the definition could be
If I understand correctly, you want to have many readers into a single destination. The architecture you created (a wrapper on std::stream with synchronized/locked writing) is not a good solution.
Here's code that doesn't work as you would expect:
With your code, the output may be:
What you need is the ability to set synchronization points wherever/whenever you want:
(this buffers everything in a
synchronize_begin
object (which can be dumped into a stream) and when it receives asynchronize_end
object, it locks amutex
(shared with othersynchronize_begin
instances) and writes toout
).or:
(synchronized is a buffer instance that exits scope at the end of the line; when it is written into out, it locks, then writes it's data.