现在的位置: 首页 > 综合 > 正文

打印线程ID

2013年01月28日 ⁄ 综合 ⁄ 共 443字 ⁄ 字号 评论关闭
#include <unistd.h>
#include <pthread.h>

pthread_t ntid;
 
void printids(const char*   s)
{
    pid_t           pid;
    pthread_t       tid;

    pid = getpid();
    tid = pthread_self();
    printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid, (unsigned int)tid, (unsigned int)tid);
}

void*   thr_fn(void*    arg)
{
    printids("new pthread: ");
    return ((void*)0);
}

int main()
{
    int err;
    err = pthread_create(&ntid, NULL, thr_fn, NULL);
    if(err != 0)
    {
        printf("can't create pthread: %s\n", strerror(err));
    }
    printids("main thread:");
    sleep(10);
    exit(0);
}

抱歉!评论已关闭.