zoukankan      html  css  js  c++  java
  • 线程相关函数(2)-pthread_self()获取调用线程ID

    获取调用线程tid

    #include <pthread.h>
    pthread_t pthread_self(void);

    示例:

    #include <pthread.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    void *printids(void *arg)
    {
        const char *str = (const char *)arg;
    
        pid_t pid;
        pthread_t tid;
        
        pid = getpid();
        tid = pthread_self();
        printf("%s pid %u tid %u (0x%x)
    ", str, (unsigned int)pid, (unsigned int)tid, (unsigned int)tid);
        
    }
    
    
    
    
    int main()
    {    
    
        pthread_t tid;
        int err;    
    
        err = pthread_create(&tid, NULL, printids, "new thread: ");
        if (err != 0) {
            fprintf(stderr, "can't create thread: %s
    ", strerror(err));
            exit(1);
        }    
        printids("main thread: ");    
        sleep(1);
        return 0;
    
    }

    运行结果:

    main thread: pid 4959 tid 9791296 (0x956740)
    new thread: pid 4959 tid 1480448 (0x169700)

  • 相关阅读:
    用 Sqlmap 识别 WAF
    OD 实验(九)
    跳转指令及其跳转条件
    Python
    Python 模块
    OD 实验(八)
    OD 实验(七)
    OD 实验(六)
    OD 实验(五)
    OD 实验(四)
  • 原文地址:https://www.cnblogs.com/yongdaimi/p/8258222.html
Copyright © 2011-2022 走看看