main thread pid is 14608 child thread pid is 14610 main thread pid is 3615 child thread pid is 3615 #include #include #include void* thread_function (void* arg) { fprintf (stderr, "child thread pid is %dn", (int) getpid ()); /* Spin forever. */ while (1); return NULL; } int main () { pthread_t thread; fprintf (stderr, "main thread pid is %dn", (int) getpid ()); pthread_create (&thread, NULL, &thread_function, NULL); /* Spin forever. */ while (1); return 0; } long tid = syscall(SYS_gettid); printf("%ldn", tid); #include #include #include #include void* thread_function (void* arg) { long tid = syscall(SYS_gettid); printf("child thread TID is %ldn", tid); fprintf (stderr, "child thread pid is %dn", (int) getpid ()); /* Spin forever. */ while (1); return NULL; } int main () { pthread_t thread; long tid = syscall(SYS_gettid); printf("main TID is %ldn", tid); fprintf (stderr, "main thread pid is %dn", (int) getpid ()); pthread_create (&thread, NULL, &thread_function, NULL); /* Spin forever. */ while (1); return 0; } main TID is 17963 main thread pid is 17963 thread TID is 17964 child thread pid is 17963 // simple program to create threads that simply sleep // compile in debian jessie with apt-get install build-essential // and then g++ -O4 -Wall -std=c++0x -pthread threads2.cpp -o threads2 #include #include #include #include // how many seconds will the threads sleep for? #define SLEEPTIME 100 // how many threads should I start? #define NUM_THREADS 25 using namespace std; // The function we want to execute on the new thread. void threadSleeper(int threadid){ // output what number thread we've created cout << "task: " << threadid << "n"; // take a nap and sleep for a while std::this_thread::sleep_for(std::chrono::seconds(SLEEPTIME)); } void main(){ // create an array of thread handles thread threadArr[NUM_THREADS]; for(int i=0;i