--------------thread_t ----线程id的理解


这个是温故而知新-------------------

#include <stdio.h>

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>


void printids(const char *s)
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s getpid()= %u pthread_self()= %u (0x%x)\n", s,(unsigned int)pid, (unsigned int)tid, (unsigned int)tid);



void *thr_fn(void *arg)
{
int i;

for(i=0;i<10;i++)
{
 sleep(1);
 printids("====new thread: ");  
}
  return((void *)0);
}
pthread_t npid;   ///这里看出只是返回一个id号, 这个号子放在那里都一样,可以放到一个函数中,随函数的退出而消失,当然也可以不要。
int main2()
{


  int err;
  err = pthread_create(&ntid, NULL, thr_fn, NULL);
  printf("can--------------------:%u\n",(unsigned int)ntid);     /////测试发现,pthread_create返回的这个pthread_t类型的id号和用pthread_self()调用后返回的id是同一个。
}
int main(void)
{
main2();
 printids("main thread:");
 sleep(1);
 pthread_join(ntid,NULL);///用来等待返回id;
}

////线程id号 类似挂号返回的条码一样, 用这个条码可以找到你的所有信息,可以做为map,list,使用。id可以是任何的一个设备,instance,资源,库等等

你可能感兴趣的:(--------------thread_t ----线程id的理解)