免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1688 | 回复: 3
打印 上一主题 下一主题

APUE中11.4节中的例子 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-10-27 09:32 |只看该作者 |倒序浏览
Figure 11.2. Printing thread IDs

#include "apue.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 thread: ");
    return((void *)0);
}

int
main(void)
{
    int     err;

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

书中讲到在 Linux 下两个线程获取到的 Pid  是不同的,但我实际运行发现 pid 是相同的,是不是因为 2.6 的内核中 pthread 的实现有所改变?

论坛徽章:
1
黑曼巴
日期:2020-02-27 22:54:26
2 [报告]
发表于 2009-10-27 09:55 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
3 [报告]
发表于 2009-10-27 10:16 |只看该作者
使用NPTL线程库,线程的pid是相同的。 可能书中所指是针对以前的linuxthreads线程库的吧,使用linuxthreads确实pid会不同。

论坛徽章:
0
4 [报告]
发表于 2009-10-27 15:39 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP