Problems with output in a multithreaded program

F

Flavio

Hi, I try to write here because maybe my problem is a common one.

I have a rather complicated multithreaded program, which uses the POSIX
pthread standard.
A master routine calls a series of slave routines.

My problems is about output. If I use the standard syntax:

.....
cout << "Hi, I am thread number" << i << endl;
.....

anything goes right. But if instead I try to send the output to a file,
opened in the usual way:

.....
out.open("Calcolo_Parallelo.log", ios::app);
.....

.....
out << "Hi, I am thread number" << i << endl;
.....

sometimes the output is not written. I haven't understood which lines
of code are written and which others not, but approximately a 50% of
the total output requested is missed (and where is it finished???).

Somebody has an idea of how to solve this problem?

Thank you,

Flavio Cimolin
 
M

mlimber

Flavio said:
Hi, I try to write here because maybe my problem is a common one.

I have a rather complicated multithreaded program, which uses the POSIX
pthread standard.
A master routine calls a series of slave routines.

My problems is about output. If I use the standard syntax:

....
cout << "Hi, I am thread number" << i << endl;
....

anything goes right. But if instead I try to send the output to a file,
opened in the usual way:

....
out.open("Calcolo_Parallelo.log", ios::app);
....

....
out << "Hi, I am thread number" << i << endl;
....

sometimes the output is not written. I haven't understood which lines
of code are written and which others not, but approximately a 50% of
the total output requested is missed (and where is it finished???).

Somebody has an idea of how to solve this problem?

You should ask in comp.programming.threads. This newsgroup deals only
with the standard C++ language proper, and it knows nothing of threads.
(Compare this FAQ:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9.)

Cheers! --M
 
D

Daniel T.

"Flavio said:
Hi, I try to write here because maybe my problem is a common one.

I have a rather complicated multithreaded program, which uses the POSIX
pthread standard.
A master routine calls a series of slave routines.

My problems is about output. If I use the standard syntax:

....
cout << "Hi, I am thread number" << i << endl;
....

anything goes right. But if instead I try to send the output to a file,
opened in the usual way:

....
out.open("Calcolo_Parallelo.log", ios::app);
....

....
out << "Hi, I am thread number" << i << endl;
....

sometimes the output is not written. I haven't understood which lines
of code are written and which others not, but approximately a 50% of
the total output requested is missed (and where is it finished???).

Somebody has an idea of how to solve this problem?

Please keep in mind that three independent output functions are being
called in your code, unless you have locked all other threads from using
'out' they could interfere with that output.

This isn't really a C++ question since C++ has no concept of threads.
You should try one of the newsgroups dedicated to multithreading.
 
F

Flavio

Please keep in mind that three independent output functions are being
called in your code, unless you have locked all other threads from using
'out' they could interfere with that output.

Yes, I also used a mutex to send a message to 'out' in order to prevent
such conflicts. But it seems that is not the problem.
This isn't really a C++ question since C++ has no concept of threads.
You should try one of the newsgroups dedicated to multithreading.

Ok, thank you, I'm going to ask to the other newsgroup.

Bye,

Flavio
 
D

Daniel T.

Please keep in mind that three independent output functions are being
called in your code, unless you have locked all other threads from using
'out' they could interfere with that output.

Yes, I also used a mutex to send a message to 'out' in order to prevent
such conflicts. But it seems that is not the problem.[/QUOTE]

It may very well be the problem. What do you think would happen to a
file is two threads try to write to it at the same time?
 
F

Flavio

It may very well be the problem. What do you think would happen to a
file is two threads try to write to it at the same time?

Sorry, I wasn't able to explain me. I used a mutex variable exactly in
order to prevent what you're saying. The code that writes on the file
looks like this:

pthread_mutex_lock (&mutexwrite);
out << "Thread master: creating thread " << t << endl;
pthread_mutex_unlock (&mutexwrite);

If instead of "out" i use "cout" everything goes well!

Hi,

Flavio
 
D

Daniel T.

It may very well be the problem. What do you think would happen to a
file is two threads try to write to it at the same time?

Sorry, I wasn't able to explain me. I used a mutex variable exactly in
order to prevent what you're saying. The code that writes on the file
looks like this:

pthread_mutex_lock (&mutexwrite);
out << "Thread master: creating thread " << t << endl;
pthread_mutex_unlock (&mutexwrite);

If instead of "out" i use "cout" everything goes well![/QUOTE]

The fact that it works for cout doesn't mean much. Outputing to the
console is likely faster than outputing to a file. Are you sure that
every reference to 'out' is guarded by the same mutex?
 
M

mlimber

Daniel said:
The fact that it works for cout doesn't mean much. Outputing to the
console is likely faster than outputing to a file. Are you sure that
every reference to 'out' is guarded by the same mutex?

More to the point: are you sure you're having this discussion in the
right newsgroup?

Cheers! --M
 
K

Kaz Kylheku

Flavio said:
Yes, I also used a mutex to send a message to 'out' in order to prevent
such conflicts. But it seems that is not the problem.

One possible problem might be that each thread has its own stream
object open on the same file. What typically happens is that each
stream is associated with some operating system open file handle which
has its own read/write pointer.

So the problem is not strictly a multithreading problem, but a problem
of multiple, independent, incoherent aliased views on the same file.

You could reproduce the same problem in a single threaded program. Open
a file twice with two stream objects. Write on one of them, then write
on the other. The second write will probably clobber the first.

Because this is not a threading problem, a mutex won't help. A mutex
does not help with the problem that two aliased, incoherent views of
the same file are being manipulated. That's not what a mutex is for.

In the UNIX filesystem, there is a special mode for keeping the
read-write pointer coherent, namely the append mode. The standard C
library has an "a" flag in fopen(), and C++ has ios::append. These
ought to map to this append mode if your operating system supports it.
In append mode, newly written data is atomically written to the end of
the file. Two processes that write to the same file through different
file descriptors that are both in append mode will not clobber each
other's data.

But really, that is more intended for multi process programming: for
instance, two or more processes spewing debugging logs to the same
shared file. Within one program, there should be no reason (bad design
not being a valid reason!) why you should open the same file more than
once at the same time whether you are multithreading or not. If you
need to refer to the same file in two places, share the stream itself.
To protect that object properly under the presence of threads, see your
platform's programming documentation.
 
F

Flavio

Kaz Kylheku ha scritto:
One possible problem might be that each thread has its own stream ....
To protect that object properly under the presence of threads, see your
platform's programming documentation.

Thank you very much for your long answer. I'm going to try to modify my
program keeping in mind what you're saying. It's just a problem of
output in a "log file", so not very important in the program, but I
like to do things well.

Bye,

Flavio
 
A

ax

Sorry, I wasn't able to explain me. I used a mutex variable exactly in
order to prevent what you're saying. The code that writes on the file
looks like this:

pthread_mutex_lock (&mutexwrite);
out << "Thread master: creating thread " << t << endl;
pthread_mutex_unlock (&mutexwrite);

If instead of "out" i use "cout" everything goes well!

because in c++ and in c cout is a file "_UNBUF" and the other no?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top