thread::join [Advanced]- Can we reuse that same thread object for different tasks after join()?

Question-4: Can we reuse that same thread object for different tasks after join()?

Yes we can use same thread object to call 3 different task one after other as given in following example.

For better understanding please copy and paste given code and try to run by yourself.

#include<iostream>
#include<thread> //Thread Header
#include<chrono>

using namespace std;

//actual task to be performed
//by our thread
void displayActivity()
{
    cout<<"www.techsujhav.com : "<<endl;
    std::this_thread::sleep_for(60s);
}

void displayTask()
{
    cout<<"www.techsujhav.com : New Task"<<endl;
    std::this_thread::sleep_for(60s);
}

int main()
{
    cout<<"Going to start thread"<<endl;
    {//New scope started.
        std::thread t_obj(displayActivity);
        cout<<"Thread Id ="<<t_obj.get_id()<<endl;

        cout<<"Before Join, (ACTIVE) joinable=" <<t_obj.joinable()<<endl;
        t_obj.join();

        cout<<"After Join, (ACTIVE) joinable=" <<t_obj.joinable()<<endl<<endl;

        cout<<"REUSING Thread Object"<<endl;

        t_obj=std::thread(displayActivity);
        cout<<"*** Thread Id ="<<t_obj.get_id()<<endl;
        cout<<"*** Before Join, (ACTIVE) joinable=" <<t_obj.joinable()<<endl;

        t_obj.join();

        cout<<"***After Join, (ACTIVE) joinable=" <<t_obj.joinable()<<endl<<endl;
        cout<<"REUSING Thread Object with NEW Task"<<endl;

        t_obj=std::thread(displayTask);
        cout<<"### Thread Id ="<<t_obj.get_id()<<endl;

        cout<<"### Before Join, (ACTIVE) joinable=" <<t_obj.joinable()<<endl;

        t_obj.join();

        cout<<"### After Join, (ACTIVE) joinable=" <<t_obj.joinable()<<endl<<endl;
        cout<<"Going out of scope"<<endl;
    }        
    cout<<"Main Exit"<<endl;
    return 0;
}

TechSujhav@TEAM:~/thread$ ./join

Going to start thread

Thread Id =140527220852480

Before Join, (ACTIVE) joinable=1

www.techsujhav.com :

After Join, (ACTIVE) joinable=0

 

REUSING Thread Object

*** Thread Id =140527220852480

*** Before Join, (ACTIVE) joinable=1

www.techsujhav.com :

***After Join, (ACTIVE) joinable=0

 

REUSING Thread Object with NEW Task

### Thread Id =140527220852480

### Before Join, (ACTIVE) joinable=1

www.techsujhav.com : New Task

### After Join, (ACTIVE) joinable=0

 

Going out of scope

Main Exit

Explanation:


Important thing to note here is thread Id is same in all 3 calls. Because we are using the same thread-object in every call

However, it does not imply that same thread is used by operating system, every time a new thread is generated. We can check this on any Linux system.

If you running following commands from other terminal, you would come to know. Please check below output.

 

TechSujhav@TEAM:~$ ps –ef |grep –i join

<to get process id of ./join>

TechSujhav@TEAM:~$ top -H -p 43835 < process id of ./join >

 

top - 21:48:21 up 187 days, 21:21,  4 users,  load average: 0.00, 0.00, 0.00

Threads:   2 total,   0 running,   2 sleeping,   0 stopped,   0 zombie

%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

KiB Mem : 19772300+total, 59427144 free,  1011180 used, 13728468+buff/cache

KiB Swap:  8388604 total,  8347236 free,    41368 used. 19526404+avail Mem

 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND

43835 TechSujhav     20   0   24376   1888   1736 S  0.0  0.0   0:00.00 join

43836 TechSujhav     20   0   24376   1888   1736 S  0.0  0.0   0:00.00 join

 

 

 

top - 21:48:45 up 187 days, 21:22,  4 users,  load average: 0.00, 0.00, 0.00

Threads:   2 total,   0 running,   2 sleeping,   0 stopped,   0 zombie

%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

KiB Mem : 19772300+total, 59427972 free,  1010344 used, 13728468+buff/cache

KiB Swap:  8388604 total,  8347236 free,    41368 used. 19526488+avail Mem

 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND

43835 TechSujhav     20   0   89912   1956   1800 S  0.0  0.0   0:00.00 join

44291 TechSujhav     20   0   89912   1956   1800 S  0.0  0.0   0:00.00 join

 

 

 

top - 21:49:51 up 187 days, 21:23,  4 users,  load average: 0.00, 0.00, 0.00

Threads:   2 total,   0 running,   2 sleeping,   0 stopped,   0 zombie

%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

KiB Mem : 19772300+total, 59428580 free,  1009708 used, 13728472+buff/cache

KiB Swap:  8388604 total,  8347236 free,    41368 used. 19526552+avail Mem

 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND

43835 TechSujhav     20   0   89912   1956   1800 S  0.0  0.0   0:00.00 join

44651 TechSujhav     20   0   89912   1956   1800 S  0.0  0.0   0:00.00 join

 

 

Question -3: What will happen if we call join again on same thread?



PREV:: What will happen if we call join again on same thread.

Your Comments /Suggestions & Questions are always welcome.

We would like to help you with best of our knowledge.

So feel free to put Questions.

No comments:

Post a Comment