stream in multiprocess (multithread) apps

M

Michael Shestero

Hello.
I have some function f(stream* out) that makes text-output into stream
(*out) i.e. (*out)<<"Hello"<<endl;. The (*out) can be cout, ofstream, a
strstream, etc. This function working in the background process (thread).
The main process need to receive this outout for the further transfering
into console-like window or something else.
How can I do this? Need I use a pipe? How should I connect the pipe not to
files, but to streams?
Please, give me a piece of sample code!
Thanx.
Shesero Michael
 
M

Moonlit

Hi,

Michael Shestero said:
Hello.
I have some function f(stream* out) that makes text-output into stream
(*out) i.e. (*out)<<"Hello"<<endl;. The (*out) can be cout, ofstream, a
strstream, etc. This function working in the background process (thread).
The main process need to receive this outout for the further transfering
into console-like window or something else.
How can I do this? Need I use a pipe? How should I connect the pipe not to
files, but to streams?
Please, give me a piece of sample code!
Thanx.
Shesero Michael
In most OS'es threads share data. So writing it into a string and reading it
from anothere thread should work. Protect the string with a mutex.

Regards, Ron AF Greve
 
M

Michael Shestero

Michael Shestero said:
In most OS'es threads share data. So writing it into a string and reading it
from anothere thread should work. Protect the string with a mutex.

Yes, these threads share memory.
The question is not general one "how to do it", but releated streams and
derived classes.
The important point is that I don't want to change f(stream* out) function
(the code isn't mine),
so I cannot use mutexes etc in it. In other words, the question was: Is it
possible to make multithreads-safe exchane through streams/iostreams or
derived (without mutexes, signals, etc) or not; and if it is possible then
how can I do it?

If it is not in general possible with streams may be it is somehow possible
with pure FILE* output?

Note: I have some reason to guess it is possible becouse of following: I
actually can redirect cout, cin and cerr for child process (that doesn't
share memory with parent) using pipes. There are several examples in
internet for Windows and Unix. For example, HTTP-server do this redirection
on calling for external cgi-program which receive parameters and push result
through stdin/stdout.
I will also be quite satisfied if I you told me how to do such thing not
with separated child process, but with thread (in shared memory).

Thanx.
Shestero Michael
 
J

Jeffrey Schwab

Michael said:
Yes, these threads share memory.
The question is not general one "how to do it", but releated streams and
derived classes.
The important point is that I don't want to change f(stream* out) function
(the code isn't mine),
so I cannot use mutexes etc in it.In other words, the question was: Is it
possible to make multithreads-safe exchane through streams/iostreams or
derived (without mutexes, signals, etc) or not; and if it is possible then
how can I do it?

The fact that you can't change the f() function doesn't mean you can't
use mutexes.

Have you got a threadsafe stringstream? If not, write one. (Hint:
wrap or derive from the non-threadsafe version.)
 
M

Michael Shestero

[...]
The fact that you can't change the f() function doesn't mean you can't
use mutexes.

Have you got a threadsafe stringstream? If not, write one. (Hint:
wrap or derive from the non-threadsafe version.)
Thanx a lot. That is exactly I am asking for.
Could anybody send me a URL with a such sample code or a guide how to write
"threadsafe stringstream".

Shestero Michael
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top