473,324 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

using streams for communication between threads...?

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

Jan 19 '06 #1
5 3465
stefan.oedenko...@gmx.de wrote:
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.

Jan 19 '06 #2

st***************@gmx.de wrote:
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)

Jan 19 '06 #3

st***************@gmx.de wrote:
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.

Jan 19 '06 #4
st***************@gmx.de wrote:
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

Jan 19 '06 #5
Thanks,
the last post is what i was searching for ;-)
regards,
Steve

Jan 20 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ayende Rahien | last post by:
Is it possible to use events as a communication mechanism between threads?
6
by: Evan David Light | last post by:
After agonizing over this problem for a few days, I've decided to seek help. No, not the variety that involes a jacket that zips up the back but this august body of intrepid individuals. I've...
2
by: | last post by:
This will most definitely seem n(oo|ew)bish but I come seeking guidance in the ways of "IO Streams". I have Bjarne's book but sadly it does not seem to delve into streams a whole (at least from a...
9
by: Emmanuel Charruau | last post by:
Hi, I am looking for a class or any information which would allow me to make communicate mini-module in c++. I have been looking on the net for some examples of such implementation, but I did...
6
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked...
9
by: Carsten H. Pedersen | last post by:
Hello. Having an issue with double streams... for a lack of a better name. :) I have the following two streams: - FooInputStream, fis, extending InputStream - FooOutputStream, fos, extending...
4
by: Ryan Liu | last post by:
TcpClient has a method called GetworkStream GetStream(); So in other words, there is only one stream associate with it for input and output, right? So while it is receiving, it can not send, and...
6
by: Jck | last post by:
Could someone tell me how a thread can send data / post message to another thread in vb.net please? I have a main thread and a socket thread. I need a way to let the main thread know when my...
6
by: Ricardo Vazquez | last post by:
Hello everybody, I'm programming a TCP server. During the stress tests I noticed that maybe my socket-receiving thread became deaf after an hour of heavy stress. I think that the reason could...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.