Hi,
I'm using boost.threads library for a project im working on, and Im having some problems designing a thread safe wrapper for std::cout. I have to do this because several of my threads need to use cout, and i need to avoid any clashes.
So, my solution is to make a dedicated singleton Output class which i think is the best thing to do? Something like this:
Then, for all my other classes I pass a pointer to this Output class in their respective constructors, and set a local private pointer to point to the Output instance, so for every new class i would do:
Am i going along the correct lines here? Or am i over complicating things with all the pointers
I basically just want to be able to use cout safely within a thread.
Thanks a lot for any tips,
Jack
I'm using boost.threads library for a project im working on, and Im having some problems designing a thread safe wrapper for std::cout. I have to do this because several of my threads need to use cout, and i need to avoid any clashes.
So, my solution is to make a dedicated singleton Output class which i think is the best thing to do? Something like this:
Code:
class Output {
private:
...
public:
void WriteData(const std::string& data_string); //Mutex's/locking etc in here
};
Then, for all my other classes I pass a pointer to this Output class in their respective constructors, and set a local private pointer to point to the Output instance, so for every new class i would do:
Code:
Output output;
...
MyClass mc(&output); // sets a pointer (*out) inside MyClass
... // Inside a thread
mc.out->WriteData("Hello World");
Am i going along the correct lines here? Or am i over complicating things with all the pointers

Thanks a lot for any tips,
Jack