I was reading threads and concurrency and hit this quote:
std::this_thread::get_id() return the id of the thread calling it.
So, I wrote the following code to test the function and see if it’s doing something else than what Uniux’s gettid()
does:
#include <iostream>
#include <thread>
#include <chrono>
#include <unistd.h>
using namespace std;
void someFunction(){
int i = 0;
while(i++ < 2){
cout<<"TID using gettid(): "<< gettid()<<endl;
cout<<"TID using this_thread::get_id: "<< this_thread::get_id()<<endl<<endl;
std::this_thread::sleep_for(chrono::milliseconds(900));
}
}
int main(){
cout<<"Hello from main"<<endl;
cout<<"TID using gettid(): "<<gettid()<<endl;
cout<<"TID using this_thread::get_id(): "<<this_thread::get_id()<<endl<<endl;
thread testThread(someFunction);
testThread.join();
return 0;
}
The output shows that the two functions are returning different values and my question is why? is this related to pths and pthreads?