Jens Luedicke Jens Luedicke - 2 months ago 9
C++ Question

How to pass parameters to a Thread object?

I'm working with a C++ class-library that provides a Thread base-class where the user has to
implement a

run()
method.

Is there a recommended way on how to pass parameters to that
run()
method? Right now
I prefer to pass them via the constructor (as pointers).

Answer

I'm not sure about C++, but that's how you would do it in Java. You'd have a class that extends Thread (or implements Runnable) and a constructor with the parameters you'd like to pass. Then, when you create the new thread, you have to pass in the arguments, and then start the thread, something like this:

Thread t = new MyThread(args...);
t.start();

Must be the same in your case.

Comments