473,466 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problems with output in a multithreaded program

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
10 1698
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
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
> 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
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
> 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
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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Elbert Lev | last post by:
I started with Python two weeks ago and already saved some time and efforts while writing 2 programs: 1. database extraction and backup tool, which runs once a month and creates a snapshot of...
5
by: Matthew Speed | last post by:
(About me: I know very little about writing server applications. I have done plenty of VB6 desktop app work but this is my first server program. I got it to work by modifying examples. I...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
3
by: | last post by:
Is it possible to have just a multithreaded sub procedure? What I need is a timer time_elapsed event (2 sec interval) send params to a sub that is multithreaded. I have a COM component used to...
3
by: Wilson | last post by:
i am very new to c++ and am creating a new program, below are two seperate parts of the program, made to run seperately, however the constructor in one works (prints "constructing" on the screen),...
9
by: Andreas Schmitt | last post by:
I am workin on a 2 part project right now. The first part is a DLL, the second part a normal exe using that DLL. When I use the VS2005 standard setting for compiling with the Multithreaded-DLL...
0
by: fabian.conrad | last post by:
Hi,. I am trying to build a multithreaded dll file, as I am new to that sort of things I've followed a tutorial step by step but still can't get things to work. I am using Visual C++ 2005 Express...
7
by: JahMic | last post by:
I'm having a problem with exec on my hosting server. Unfortunately, the hosting support seems to be anything but helpful. The following works fine on my localhost: <?php $MaskData =...
4
by: vmpstr | last post by:
Hi, I am working on a multithreaded application, which also happens to use STL quite heavily. I'm running into the following problem (I'm sorry I can't post any code, because it is quite...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.