Reputation: 467
I want to run two threads, one for each class I have. But I want them to be indepedent. For example, I have something like that:
class A
{
pthread_t threadA;
void runThreadA() { pthread_create(... arguments_here ...)};
}
class B
{
pthread_t threadB;
void runThreadB() { pthread_create(... arguments_here ...)};
}
Currently I create and run the two threads internally for each class, but my main is like this:
main()
{
A.runThreadA();
B.runThreadB();
pthread_join(threadA, NULL);
pthread_join(threadB, NULL);
return 0;
}
How can I join the two threads also internally, causing the two threads two run in parallel?
Hope I am understandable.. Thank you
Upvotes: 1
Views: 49
Reputation: 457
I'm going to leave this as an answer because I don't have privileges yet to comment, but I'll give it my best shot. I'm going to interpret that when you say join threads internally, that you mean inside the class and automatically, without having to write the extra join lines in the main function. In short, the answer is yes. You can call join from inside of a class, but it is somewhat ineffectual to do so, for the following reason.
Lets consider this example (I'm going to use std::thread because I'm more comfortable with it, but pthread will work very much the same):
class1
{
std::thread *t1;
void DoWork1();
void CreateThread1() {
t1= new std::thread(&class1::DoWork1, this)
JoinThread1();
}
void JoinThread1(){
if (t1.joinable()) {
t1.join();
}
}
};
class2
{
std::thread *t2;
void DoWork2();
void CreateThread2() {
t2= new std::thread(&class2::DoWork2, this)
JoinThread2();
}
void JoinThread2(){
if (t2.joinable()) {
t2.join();
}
}
};
Now we have theoretically completed this goal that every time we call Class1::CreateThread()
it will automatically join as well without the added need of that join()
call in the main thread. Except we have to remember that join()
is a blocking function. Thus, if we call the Class1::CreateThread()
in the main()
then it will block until the thread is finished and joined and only then would be be able to call Class2::CreateThread()
. So, if we tried this method, it should be obvious that the threads are not running in parallel nor is there any benefit at all to having used threads in the first place.
There is an argument for calling join()
in the destructor of the class, but you would still need to call that destructor somewhere so we're still not getting that same automation that I think you're looking for.
That example was exceedingly simple, but it hopefully shows something important. That no matter where in the class you implement this functionality to join the thread, it will either block you from using your main thread to do other things like create a second thread (thus defeating much of the purpose of threads), or you will have to call this member function that joins the thread manually from your main anyway, the only difference would be that it would be a member function of the class rather than as you have it in the above example.
TLDR Because of the fact that join()
is a blocking function it will have to be called "manually" in order to ensure that you can get any benefit out of the thread.
Upvotes: 1