10

So my question actually has several parts:

Using the Poco Threading Library:

  1. What are all of the possible methods for passing data to threads (at both thread invocation and for an already running thread).
  2. Which methods are preferred by you and why? Can you provide any additional information about your experience using these methods?
  3. Which methods are recommended by Applied Informatics (the author of Poco)? Is there any additional documentation supplied by Applied Informatics that outlines passing arguments to threads?

I've looked here already:

Thanks in advance...

Homer6
  • 15,034
  • 11
  • 61
  • 81

1 Answers1

19

The canonical way of passing arguments to a new thread is via the Runnable subclass you'll need to create as thread entry point. Example:

class MyThread: public Poco::Runnable
{
public:
    MyThread(const std::string& arg1, int arg2):
        _arg1(arg1),
        _arg2(arg2)
    {
    }

    void run()
    {
        // use _arg1 and _arg2;
        //...
    }

private:
    std::string _arg1;
    int _arg2;
};

//...

MyThread myThread("foo", 42);
Poco::Thread thread;
thread.start(myThread);
thread.join();

For passing data to an already running thread, what's the best solution depends on your requirements. For a typical worker thread scenario, consider using Poco::NotificationQueue. A complete sample with explanations can be found here: http://pocoproject.org/slides/090-NotificationsEvents.pdf

Günter Obiltschnig
  • 1,536
  • 11
  • 13
  • 2
    From the man himself. Günter is the founder of Applied Informatics and a Project Lead on the Poco C++ Library. Thank you very much, Günter, for taking the time to answer this question! – Homer6 Jul 26 '12 at 00:32
  • For people that come across this post, I also found this link to be useful: http://www.cs.bgu.ac.il/~spl111/PracticalSession09_-_Threads_Using_POCO – Homer6 Jul 26 '12 at 00:46