By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,128 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,128 IT Pros & Developers. It's quick & easy.

Problems with output in a multithreaded program

P: n/a
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

Jun 30 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Flavio wrote:
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-lit...t.html#faq-5.9.)

Cheers! --M

Jun 30 '06 #2

P: n/a
In article <11*********************@p79g2000cwp.googlegroups. com>,
"Flavio" <fl************@gmail.com> wrote:
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.
Jun 30 '06 #3

P: n/a
> 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

Jun 30 '06 #4

P: n/a
In article <11*********************@i40g2000cwc.googlegroups. com>,
"Flavio" <fl************@gmail.com> wrote:
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.


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?
Jun 30 '06 #5

P: n/a
> 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

Jun 30 '06 #6

P: n/a
In article <11**********************@d56g2000cwd.googlegroups .com>,
"Flavio" <fl************@gmail.com> wrote:
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!


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?
Jun 30 '06 #7

P: n/a
Daniel T. wrote:
In article <11**********************@d56g2000cwd.googlegroups .com>,
"Flavio" <fl************@gmail.com> wrote:
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!


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

Jun 30 '06 #8

P: n/a
Flavio wrote:
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.


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.

Jun 30 '06 #9

P: n/a

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

Jul 1 '06 #10

P: n/a
ax
On 30 Jun 2006 09:51:40 -0700, "Flavio" <fl************@gmail.com>
wrote:
>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!
because in c++ and in c cout is a file "_UNBUF" and the other no?
>Hi,

Flavio
Jul 2 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.