Can I use MT-safe stream as a pipe between two threads? A TANGLED question about standart C++-librar

M

Michael Shestero

Hello.

General description of my knotty problem:
I have a code (a function f) on standart C++ that perform some smart action.
This code works as console application getting user input from fstream and
putting output to another fstream. So, I can make easy console-application
with void main(void) { f(&cin, &cout); }.
This function f(...) works well. but it is very complex and it uses 'in' and
'out' in many places. It also sends them in depth of calling subfunctions so
I cannot just overwrite all places wher it calls (*out)<<... or (*in)>>...
operators.
Then I want to improve user interface connect this code to a
platform-depended (for example) MS Windows application, that will use MFC
CEdit-derived object as text-console. For this purpose I apparently have to
start f(...) in background and communicate with it somehow via 'in' and
'out' pointers, using them as unidirectional FIFO buffers: 'in' from user
interface to f, and 'out' from f to user interface.
Note: I actually can do the redirection of cout, cin and cerr streams for
child process (not a thread, but one that doesn't share memory with parent)
using three pipes with six FILE*-s in them. There are several examples in
Internet for Windows and Unix. For example, HTTP-server do such redirection
on calling for external CGI-program which receive parameters and push result
through stdin/stdout. But for another purpose I DO NOT want to compile f
into separated executable module (1). I also want f share memory with user
interface module (2).
HOW CAN I DO IT???

Partial task:
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 (a thread).
The main process need to receive this outout for the further transfering
into console-like window or something else. The important point is that I
don't want to change f(stream* out) function (the code isn't mine). How can
I do this?
As I undestand there are threadsafe streams neither in GNU C++ nor MS VC++
library, aren't they? I guess I can write mine if I derive a new class from
the
strstream (stringstream) class and in write constuctor which initalizes a
mutex, and
overload each function to enter a critical section, call the base operation,
and then leave the critical section. And have the destructor clean up the
mutex. Well, I can do it (although I would like prefer to get such class
from
Internet instead writing it. Does anybody know where I can download it?),
but how I shall
connect it to the main process then? Just simple read and write in it or I
need something special like a pipe?

Shestero Michael
 
R

Ron Natalie

Michael Shestero said:
So, I can make easy console-application
with void main(void) { f(&cin, &cout); }.

Ill-formed. main must return int :)
HOW CAN I DO IT???
I'm not sure I fully understand all the nuances of your problem, but the
answer, I believe, is that you want to define your own streambuf class
to direct the I/O to the right place. This can then be used to create
a normal stream that who can be converted to ostream& or whatever
that will do what you want.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top