473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

iostreams in multithreaded environment

Hi,

How does (Can?) the stream mechanism work in multithreaded programs?

I want to implement cout in my multithreaded program.

This cout stream is used for my debug information.

I don't want these messages to get shuffled when a thread switch takes
place.

The threads cannot be blocked, so I probably need to store cout messages
from each thread in a separate buffer.

Any leads or suggestions for this problem?

Greetings Anthony



Jul 19 '05 #1
3 5296
"Anthony" <an*****@EdAsys.E-is-A.A-is-E.nl> wrote...
How does (Can?) the stream mechanism work in multithreaded programs?
It is unspecified by the Standard. You can probably find more info
in the documentation to your compiler.
I want to implement cout in my multithreaded program.
That statement is unclear. 'cout' is the name of a standard output
stream. It is implemented by the vendor of the standard library
you're using. You shouldn't have to implement it yourself. Perhaps
you mean that you want to do some outputting in every thread...
This cout stream is used for my debug information.

I don't want these messages to get shuffled when a thread switch takes
place.

The threads cannot be blocked, so I probably need to store cout messages
from each thread in a separate buffer.

Any leads or suggestions for this problem?


Every thread should have its own _stringstream_ for output and
then stuff it into 'cout' right before finishing its work. That
will assure that all output of each thread will be in one place.

Unfortunately, that means that you cannot use 'std::cout << ...'
to output your debug information, but you should be able to write
some kind of a macro to do your debugging output (which would
also allow you to disable the output when done debugging) with
the help of the thread ID or some such.

Since C++ and multithreading are orthogonal (the Standard does
not define anything thread-related), you might find more useful
information in comp.programming.threads.

Victor
Jul 19 '05 #2
On Wed, 8 Oct 2003 14:45:32 +0200, "Anthony"
<an*****@EdAsys.E-is-A.A-is-E.nl> wrote:
Hi,

How does (Can?) the stream mechanism work in multithreaded programs?
By appropriately locking access to shared streams.
I want to implement cout in my multithreaded program.

This cout stream is used for my debug information.

I don't want these messages to get shuffled when a thread switch takes
place.

The threads cannot be blocked, so I probably need to store cout messages
from each thread in a separate buffer.

Any leads or suggestions for this problem?


Log through a proxy that locks a mutex. e.g.

log(WARNING).get() << "This appears" << " then this" <<
" regardless of other threads using the log.\n";

(or use
#define LOG(level) log(level).get() << __FILE__ << ':' << __LINE__
or similar).

The log function will return a proxy object that locks a mutex in the
constructor and unlocks it in the destructor when the last reference
is destroyed (using reference counting).

I believe James Kanze has such a logging library:
http://www.gabi-soft.fr/codebase-en.html

Tom
Jul 19 '05 #3
> I want to implement cout in my multithreaded program.

This cout stream is used for my debug information.

I don't want these messages to get shuffled when a thread switch takes
place.

The threads cannot be blocked, so I probably need to store cout messages
from each thread in a separate buffer.

Any leads or suggestions for this problem?


Anthony,

I posted a few days ago a solution in the comp.lang.c++.moderated
group, along with a solution for use in Windows. Basically, the trick
is to use a macro with a locking class that uses the RAII idion
(Resource Acquisition Is Initialization): it locks in the constructor
and unlocks in the destructor.

A macro could look like this:

extern SynchronizationPrimitive synch_prim;
#define CERR (Lock(synch_prim), cerr)

When you write something like:

CERR << "This is a test" << endl;

and since the return value of this expression is the rightmost value,
effectively you get something like this:

1. leftmost expression is evaluated: lock synchronization primitive
2. right-most expression is evaluated: cerr and friends to its right
3. synchronization primitive is unlocked.

I posted a complete example that I obtained from this site, books and
magazines and I posted the example in the moderated newsgroup. Search
in that newsgroup for the complete source code and let me
know--through this newsgroup--if I can be of further assistance.

Regards,

Javier.
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: pradyumna | last post by:
In Project settins - C/C++ - Code Generation, what is the difference between the option "Multithreaded" and "Multithreaded DLL". I understand that on selecting multithreaded option, single and...
6
by: Dan Kelley | last post by:
We have a multithreaded app that responds to events, and writes these events to a text file. This text file is used by an external system for further processing. We want to be able to write...
10
by: Dave O'Hearn | last post by:
I want to open a file for both reading and writing, but when I do "ios::in|ios::out" with an fstream, it creates the file if it doesn't already exist. I don't like that last part. C's fopen has the...
9
by: | last post by:
There are two methods of snagging a 'line' from a stream (with an attached streambuf that reads/writes from a socket). One is the std function "getline" and the other is the "getline" method of...
7
by: Pavils Jurjans | last post by:
Hello, I wanted to get some light in the subject. As I develop ASP.NET applications, it's necessary to understand how exactly the server- communication happens. It seems like initially...
4
by: Richard Bell | last post by:
I'm new to VB.net and have a multithreaded application that needs to display a VB form with a cancel button. The thread that displays the form needs to set a cancel flag if the button is pressed. ...
9
by: nicolas.michel.lava | last post by:
Hi there, I have some trouble using STL containers. I'm working on a multithreaded (pthread) application making heavy usage of STL containers. All accesses to containers are made in locked...
6
by: Stephen Carson | last post by:
I'm trying to build a Web Service that will kick off threads as logging requests come in. These threads will then log to the database. I have been able to make a simple Web Service. I have been...
0
by: shorti | last post by:
DB1 V8.2 DB2 Info Center has information on the subject that states in a multi function environment to include the EXEC SQL INCLUDE SQLCA statement in only one function and the remaining source...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.