linuxtid.cpp 473 B

12345678910111213141516171819202122232425262728
  1. #include <sys/syscall.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <pthread.h>
  5. void* thread_proc(void* arg)
  6. {
  7. pthread_t* tid1 = (pthread_t*)arg;
  8. int tid2 = syscall(SYS_gettid);
  9. pthread_t tid3 = pthread_self();
  10. while(true)
  11. {
  12. printf("tid1: %ld, tid2: %ld, tid3: %ld\n", *tid1, tid2, tid3);
  13. sleep(1);
  14. }
  15. }
  16. int main()
  17. {
  18. pthread_t tid;
  19. pthread_create(&tid, NULL, thread_proc, &tid);
  20. pthread_join(tid, NULL);
  21. return 0;
  22. }