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

stream in multiprocess (multithread) apps

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

Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi,

"Michael Shestero" <sh******@cyber.miem.edu.ru> wrote in message
news:br************@ID-54498.news.uni-berlin.de...
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
Jul 22 '05 #2

P: n/a
> "Michael Shestero" <sh******@cyber.miem.edu.ru> wrote in message
news:br************@ID-54498.news.uni-berlin.de...
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!
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.


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

Jul 22 '05 #3

P: n/a
Michael Shestero wrote:
"Michael Shestero" <sh******@cyber.miem.edu.ru> wrote in message
news:br************@ID-54498.news.uni-berlin.de...
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!
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.

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.)

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


Jul 22 '05 #4

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

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.