thread-safe iostreams - ideas for implementation ? I am currently experimenting with the boosth thread/mutex libraries, and did notice that the standard io streams are not thread safe. In the boost examples, this is handles by some global mutexes which are locked before writing to cout, cerr. I would like to have some classes which - behave exactly as cout, cerr and named file out streams - are thread safe mutex locked Are there any ideas on how to do this ?? I have the following possibilities in mind: 1) make a wrapper class around ostreams which have assigned a specific file or ostream as a member, make each input to operator<<() locked by a lock to a static mutex. advantage: not that hard to implement disadvantage: when outputting multiple things, like in mutex-cout << 'A' << 'B' << std::endl; another thread can come in between 'A' and 'B'. Not really thread safe, as I would like to have the entire line protected. 2) Make some trickery with manipulators and use the std:: stremas, something like cout << lock << 'A' << 'B' << std::endl << unlock ; advantage: pretty ok syntax (albeit one can forget to unlock!) disadvantage: I really have no idea how to implement in a niece and easy way .. 3) Make some trickery with out-put buffering something like having a class modeled like a stream_buff, which is large enough to hold what's written to the associated streams, and which does write to the cout or file streams in a mutex-locked fashion whenever it's manually flushed. ??? silly or possible ?? 4) finaly, a class having a boring print(std::string) function, which sets a lock on the static mutex advantage: easy-peasy disadvantage: C-print like, needs a new class type for each out-file, as a new static mutex has to be assigned to each out-file No nice C++ straming possible whatever I want to write needs to be converted into a std::string first. implementatio: can be templated by a string parameter which is the name of the file, or 'cout'/'cerr' Probably my ideas are not very bright, so I would appreciate some input from boost::thread users, which undoubtely have met the same problem before. Cheers, Marc -- Marc Cromme, cand. polyt, Ph.D Senior Developer, Project Manager Index Data Aps KĂžbmagergade 43, 2 1150 Copenhagen K. Denmark tel: +45 3341 0100 fax: +45 3341 0101 http://www.indexdata.com INDEX DATA Means Business for Open Source and Open Standards