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

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

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

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


P: n/a

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

Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.