We're currently trying to do some multithreaded work using pthreads
We have a class that looks like this.
class TestObj{
public:
void runThreaded1(int x, int y);
}
The class holds two functions which we need to run in a multithreaded environment, i.e have multiple threads running "runThreaded1".
We have another class which looks something like this:
class RunThreaded{
public:
RunThreaded(ThreadObj& object);
void run(int x, int y);
}
We would like to have multiple instances of the RunThreaded class. Each instance would have its own pthread that runs the implementation inside the ThreadObject that is passed on to it.
So essentially a run will look like this:
(let's say we have an instance of ThreadObj named obj in hand)
RunThreaded t1 = new RunThreaded(obj);
RunThreaded t2 = new RunThreaded(obj);
t1.run(4, 5);
t2.run(6, 7);
t1.run(8, 9);
t2.run(10, 11);
So in the example above we have two objects that each run the same implementation of runThreaded1 but on two different pthreads
Two questions:
1) What is the best way to create and use the pthread with our constraints? (i.e running a thread on an implementation of a function that is a member of some object). We found this question after some googling: pthread function from a class and wonder if those are the only solutions for doing it. We started implementing it but ran into some issues
2) Is it possible to create the pthread once and then call the run method every time without using pthread_create? We know that pthread_create creates and runs the thread. We want to create the thread only once but be able to "run" the threaded procedure multiple times but don't know if such a pthread_run function exists
Thank you very much!