Re: Mutex in ostream operator - doesn't work
Hansel Stroem wrote:
Good evening dear newsgroup (and sorry for cross-posting with .threads),
I am trying to synchronize output to ostreams such as cout and cerr for
threaded logging purposes. And it doesn't seem to work. The blurb of code
below is supposed to do following :
ostream& operator <<(ostream& out, StreamLocker& SL)
{
if (not_locked)
{
pthread_mutex_lock(this_lock);
not_lock = false;
}
else // if locked
{
pthread_mutex_unlock(this_lock);
not_lock = true;
}
return out;
}
Rest of code is debugging messages and attempts to get it to work with
flush() et al. Nothing works; output is as mingled as it could be. Please
help me kindly.
Thank you,
Not A Coder.
-----------------------------------------------
#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;
static pthread_mutex_t stderr_lock;
static pthread_mutex_t stdout_lock;
class StreamLocker
{
public:
bool locked;
pthread_mutex_t my_lock;
StreamLocker();
void InitLock(pthread_mutex_t mutex_lock);
friend ostream& operator<<(ostream& out, StreamLocker &sl);
};
static StreamLocker SL;
StreamLocker::StreamLocker()
{
cout << "Creating StreamLocker" << endl;
this->locked = false;
pthread_mutex_init(&my_lock, NULL);
pthread_mutex_init(&stderr_lock, NULL);
pthread_mutex_init(&stdout_lock, NULL);
cout << "Created StreamLocker" << endl;
}
ostream& operator <<(ostream& out, StreamLocker &sl)
{
if (!sl.locked)
{
if (out == cerr)
{
out << "LK ER";
pthread_mutex_lock(&stderr_lock);
}
else if (out == cout)
{
out.flush();
out << "*LK OU*";
if ( pthread_mutex_lock(&stdout_lock) != 0 )
perror("Couldn't obtain lock");
}
else
{
out << "LK SE";
pthread_mutex_lock( &(sl.my_lock) );
}
}
else
{
out.flush();
if (out == cerr)
{
out << "ULK ER";
pthread_mutex_unlock(&stderr_lock);
}
else if (out == cout)
{
out << "*ULK OU*";
if ( pthread_mutex_unlock(&stdout_lock) != 0 )
perror("Couldn't unlock");
}
else
{
out << "ULK SE";
pthread_mutex_unlock(& (sl.my_lock)) ;
}
}
sl.locked = !sl.locked;
// out.flush();
return out;
}
void StreamLocker::InitLock(pthread_mutex_t mutex_lock)
{
pthread_mutex_init(&mutex_lock, NULL);
}
typedef void* (*thread_body) (void*) ;
template<int N>
void* Thread_Body(void* arg)
{
unsigned long cycle = 1;
while ( ++cycle > 0)
{
cout << SL ;
cout << "Thread ## " << N << " in cycle ## " << cycle << " speaking with
id " << pthread_self() << endl ;
cout << SL;
if ( (random() % 111)==0 )
sleep( 1 );
}
}
int main(int argc, char** argv)
{
pthread_attr_t thread_attr;
pthread_attr_init(&thread_attr);
pthread_t thread_id;
thread_body tb_1 = Thread_Body<1>, tb_2 = Thread_Body<2> , tb_3 =
Thread_Body<3>;
pthread_create(&thread_id, &thread_attr, tb_1, NULL);
pthread_create(&thread_id, &thread_attr, tb_2, NULL);
pthread_create(&thread_id, &thread_attr, tb_1, NULL);
pthread_create(&thread_id, &thread_attr, tb_3, NULL);
pthread_create(&thread_id, &thread_attr, tb_2, NULL);
pthread_create(&thread_id, &thread_attr, Thread_Body<7>, NULL);
pthread_join(thread_id, NULL);
}
Not sure it is C++ question.. anyway, one thing that struck me
immediately (never had time to look through the whole code) is that the
code checks and sets the shared sl.locked outside of the mutex. Which
means, the threads will often be fooled to assume the mutex is not
locked when it is and other way around.. not that I advise to
sycnrhonize `locked' or even use it at all; I just believe it is one of
the problems in the code.
My advice would be:
Use thread-local or just local ostrstream (yes, the deprecated one, from
Appendix D, the new ostringstream is even slower than ostrstream) then
flush the log record onto the device in a single system call or under
mutex if you want to make sure it is written in full and you have to use
a loop of system calls for that on your system.
-Pavel