The difference between the thread pthread_t and pid_t

Thread ID meaning
pthread_t Only, it may be the same in different processes within the process.
pid_t Globally unique, different processes are not the same. And the cycle is an incremental allocation method, start multiple threads within a short time will have a different thread id.

Test procedures are as follows:

#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>
#define gettid() syscall(__NR_gettid)
void* threadFunc(void *){
	pid_t  t = gettid();
	printf("gettid: %lx\n", t);
}

int main()
{
	pthread_t t1, t2;
	pthread_create(&t1, NULL, threadFunc, NULL);
	printf("%lx\n", t1);
	pthread_join(t1, NULL);
	
	pthread_create(&t2, NULL, threadFunc, NULL);
	printf("%lx\n", t2);
	pthread_join(t2, NULL);

    return 0;
}

Here Insert Picture Description


Reference "Linux multi-threaded server-side programming."

Published 92 original articles · won praise 2 · Views 3411

Guess you like

Origin blog.csdn.net/zxc120389574/article/details/105201003