曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 11788|回复: 0
打印 上一主题 下一主题

[线程] pthread_self() --- 获取进程 ID

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
跳转到指定楼层
楼主
发表于 2010-6-14 12:00:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
原型
#include <pthread.h>
pthread_t pthread_self(void);

说明
获取本进程自身的 ID。进程 ID 类型是 pthread_t ,这个类型一般为long long 型,8个字节。

测试代码
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *thread_one ()
{
    printf ("thread_one id is %lld\n", pthread_self());
}

void *thread_two()
{
    printf ("thread_two id is %lld\n", pthread_self());
}

int main (int argc, char **argv)
{
    pid_t pid;
    pthread_t tid_one, tid_two;
   
    if ((pid = fork()) == -1) {
        perror ("fork");
        exit (EXIT_FAILURE);
    } else if (pid == 0) {
        pthread_create (&tid_one, NULL, (void *)thread_one, NULL);
        pthread_join (tid_one, NULL);
    } else {
        pthread_create (&tid_two, NULL, (void *)thread_one, NULL);
        pthread_join (tid_two, NULL);
    }

    wait (NULL);
}
运行与输出
 ./pthread_self
thread_one id is 3077766000
thread_one id is 3077766000
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-5-3 23:28 , Processed in 0.075989 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表