using streams for communication between threads...?

S

stefan.oedenkoven

Hi ng,

i would like to exchange some data between two threads... It would be
comfortable to let Thread A push some data via putc(aStream) and let
Thread B listen to aStream.

Is it possible to create such a stream without using a physical file or
something else which behaves like this?
e.g.:
FILE * pFile;
pFile = fopen("i_dont_want_physical_file_here","r+");
fread(buffer, tokenlength, count, pFile)

thanks in advance,
steve
 
S

Shark

Hi ng,

i would like to exchange some data between two threads... It would be
comfortable to let Thread A push some data via putc(aStream) and let
Thread B listen to aStream.

Is it possible to create such a stream without using a physical file or
something else which behaves like this?
e.g.:
FILE * pFile;
pFile = fopen("i_dont_want_physical_file_here","r+");
fread(buffer, tokenlength, count, pFile)

thanks in advance,
steve

Most threading models have threads read from a shared memory location
that your process makes available to them. You can find c++ or c
libraries that enable you to do this but they are specific to the
platform you are on. C++ does not have anything that specifies this.
 
M

Maxim Yegorushkin

Hi ng,

i would like to exchange some data between two threads... It would be
comfortable to let Thread A push some data via putc(aStream) and let
Thread B listen to aStream.

Is it possible to create such a stream without using a physical file or
something else which behaves like this?

In Unix certainly. Use local sockets or pipes.

man unix(7)
man pipe(2)
 
E

Earl Purple

Hi ng,

i would like to exchange some data between two threads... It would be
comfortable to let Thread A push some data via putc(aStream) and let
Thread B listen to aStream.

Is it possible to create such a stream without using a physical file or
something else which behaves like this?
e.g.:
FILE * pFile;
pFile = fopen("i_dont_want_physical_file_here","r+");
fread(buffer, tokenlength, count, pFile)

thanks in advance,
steve

If you want to do it with iostream then you should create a derivation
from streambuf (or even better basic_streambuf) and implement all the
virtual functions of streambuf appropriately.

Then create a derivations of basic_istream basic_ostream etc. All they
need to do is construct and destruct. All the actual streaming is done
through the streambuf. When it constructs it should construct its base
class with a pointer to your streambuf.
 
?

=?iso-8859-1?q?Stephan_Br=F6nnimann?=

Hi ng,

i would like to exchange some data between two threads... It would be
comfortable to let Thread A push some data via putc(aStream) and let
Thread B listen to aStream.

Is it possible to create such a stream without using a physical file or
something else which behaves like this?
e.g.:
FILE * pFile;
pFile = fopen("i_dont_want_physical_file_here","r+");
fread(buffer, tokenlength, count, pFile)

thanks in advance,
steve

You might think of using std::stringstream, something like:
#include <iostream>
#include <sstream>

int main()
{
std::stringstream s;
char c = 'A';
const char e = 'H';
while (c <= e) {
for (int i = 0; i < 3 && c <= e; ++i) {
s << c++;
}
while (s) {
char r;
s >> r;
if (s) std::cerr << "read " << r << "\n";
}
s.clear();
}
}

You'll need to protect the access to the (shared) std::stringstream,
also I suspect that the reading thread should block until input becomes
available.
This means there must be a control class ... and soon you'll find out
that it's better to use std::queue<std::string> inside this class.

Regards, Stephan
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top