knowledge-database (beta)

Current group: codewarrior.windows

cout and multiple threads

cout and multiple threads  
SiamGod
From:SiamGod
Subject:cout and multiple threads
Date:6 Jan 2005 12:23:15 -0800
I have an application on windows that uses multiple threads, but when
ever I
cout << "Write Something" << endl;

the I/O stops working. I created a SafeCout class that should work but
does not.

class SafeCoutObj {
public:
stringstream Out;

SafeCoutObj & operator << (SafeCoutObj& (*pf)(SafeCoutObj&))
{ return pf (*this)}

SafeCoutObj & endl()
{
lock(Mutex);
printf("%s", Out.str().c_str());
Out.str()"");
unlock(Mutex);
return *this;
}
}

template
inline SafeCoutObj& operator << (SafeCoutObj& os, Type val)
{
os.Out << val;
return os;
}

inline SafeCoutObj& endl(SafeCoutObj& os)
{
os.endl();
return os;
}

Each thread has its own local copy of SafeCout it writes to.

When I run, everything starts out okay, but eventually, the endl
function puts out the same line twice, and everything dies.

Any thoughts? Is this a Codewarrior cout limitation or multi-thread
I/O problem?

Thanks
   

Copyright © 2006 knowledge-database   -   All rights reserved