[RESOLVED] Two-way event-driven communication between objects (similar to producer-consumer)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: [RESOLVED] Two-way event-driven communication between objects (similar to producer-consumer)

  1. #1
    Join Date
    Jan 2012
    Location
    USA
    Posts
    89

    [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

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,012

    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

  3. #3
    Join Date
    Jan 2012
    Location
    USA
    Posts
    89

    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.

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,827

    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.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,827

    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.

  6. #6
    Join Date
    Jul 2013
    Posts
    247

    Re: [EDITED] Two-way asynchronous communication between objects

    Quote Originally Posted by vincegata View Post
    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 05:32 PM.

  7. #7
    Join Date
    Jan 2012
    Location
    USA
    Posts
    89

    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!

  8. #8
    Join Date
    Jul 2013
    Posts
    247

    Re: Two-way event-driven communication between objects (similar to producer-consumer)

    Quote Originally Posted by vincegata View Post
    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.

  9. #9
    Join Date
    Jan 2012
    Location
    USA
    Posts
    89

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center