-
October 26th, 2013, 01:01 PM
#1
[RESOLVED] Two-way event-driven communication between objects (similar to producer-consumer)
Hi,
I need to implement a two-way asynchronous communication between two objects, not using any IPC.
What I have now is a producer-consumer model where producer sends a number "88" to consumer at random intervals. Now, I need consumer to send a number "99" back to producer at a random intervals, this probably should be done in a separate thread. My prototype could be totally wrong for what I need hence I am open to any suggestions. I'd appreciate any help, thank you.
I am using GNU on Linux, here is the compile line.
Code:
g++ -g -std=c++11 main.cpp -o main
Code:
#include<iostream>
#include<chrono>
#include<thread>
class IObject
{
public:
virtual void fromApp(int i) = 0;
virtual int toApp(int& i) = 0; // ???
};
class Producer
{
private:
IObject& obj;
public:
Producer(IObject& p) : obj(p) { }
void run() { while(1) {int d=rand()%500+100; std::this_thread::sleep_for(std::chrono::milliseconds(d)); obj.fromApp(88);} } // Generate and send data.
};
class Consumer : public IObject
{
public:
void fromApp(int i) { std::cout << i*10 << std::endl; } // Process data.
int toApp(int& i) {while(1) {int d=rand()%500+100; std::this_thread::sleep_for(std::chrono::milliseconds(d)); i = 99;} }
};
int main()
{
Consumer cons;
Producer prod(cons);
prod.run();
}
Last edited by vincegata; October 27th, 2013 at 11:09 AM.
Reason: rephrased
-
October 27th, 2013, 05:11 AM
#2
Re: Two-way event-driven communication between objects (similar to producer-consumer)
I think you'll get better help if you describe what you actually want to do. Your example doesn't make much sense, since toApp is not called from anywhere.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
October 27th, 2013, 10:22 AM
#3
Re: Two-way event-driven communication between objects (similar to producer-consumer)
I updated my post, toApp() is the function I need to be implemented.
-
October 29th, 2013, 08:26 AM
#4
Re: Two-way event-driven communication between objects (similar to producer-consumer)
ANd how is any of this even remotely "asynchronous" ?
Asynchronous means that there will need to be some form of buffering on the producing side (the producer produces something when the consumer didn't ask for it). So any produced items will need to be "stored" somewhere until a consumer has had an opportunity to consume it.
And the consumer will either have polling to see if something has been produced (akin to a messageloop) or it will be waiting for a signal from the producer by waiting on a synchronisation object. Or some hybrid of both, polling for a certain time to fall asleep in a wait loop after a period of non activity from the producer.
-
October 29th, 2013, 08:27 AM
#5
Re: Two-way event-driven communication between objects (similar to producer-consumer)
Also... you're going to need to have the producer and consumer on different threads, or the whole idea of asynchronous behaviour is not goign to make a whole lot of sense.
-
November 5th, 2013, 06:30 PM
#6
Re: [EDITED] Two-way asynchronous communication between objects
Originally Posted by vincegata
I need to implement a two-way asynchronous communication between two objects, not using any IPC.
You can use the threadsafe blocking queue implementation I posted in another of your threads, here
http://forums.codeguru.com/showthread.php?540349
Or rather you need two of these queues. They can be used to set up a two-way communication between your Producer and Consumer classes (which must run in separate threads (of which one may be the main thread)).
Last edited by razzle; November 5th, 2013 at 06:32 PM.
-
November 11th, 2013, 08:34 PM
#7
Re: Two-way event-driven communication between objects (similar to producer-consumer)
Sorry for coming back so late, I've been busy at work over timing. I know there should be a buffer for an asynchronous communication, I am just trying to figure out in which way the classes are aware of each other.
I am going to rethink what I need and I will close this thread for now.
Thank you for your responses!
-
November 12th, 2013, 08:34 PM
#8
Re: Two-way event-driven communication between objects (similar to producer-consumer)
Originally Posted by vincegata
I am going to rethink what I need and I will close this thread for now.
Yes it's obviously much better if you think before you ask.
But instead of asking detail questions about solutions you have in mind why not simply reveal up front what it is you want to accomplish. Top down problem solving and then bottom up coding is much more efficient.
This "guess what I want and try to please me" game is so annoying.
-
November 16th, 2013, 02:09 PM
#9
Re: [RESOLVED] Two-way event-driven communication between objects (similar to produce
@razzle - I asked a specific question and if you don't know the answer you just move on - it's that simple. I closed the thread because my requirements has changed.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|