gettid 和pthread_self的区别
The man page for gettid says:
The thread ID returned by this call is not the same thing as a POSIX thread ID
(i.e., the opaque value returned by pthread_self(3)).
看来,线程的id,在linux中分为POSIX thread ID , 和内核中对每一个线程的id.
gettid是linux 的一个系统调用, 查看sys_gettid
/* Thread ID - the internal kernel "pid" */
asmlinkage long sys_gettid(void)
{
return task_pid_vnr(current);
}
struct upid {
/* Try to keep pid_chain in the same cacheline as nr for find_pid */
int nr;
struct pid_namespace *ns;
struct hlist_node pid_chain;
};
struct pid
{
atomic_t count;
/* lists of tasks that use this pid */
struct hlist_head tasks[PIDTYPE_MAX];
struct rcu_head rcu;
int level;
struct upid numbers[1];
};
这里面的level不清楚是什么含义? 有知道的告诉我,多谢了。
问:_syscall[0-6]跑哪里去了
答:没有了
问:那现在用啥呢?
答:用系统调用 syscall(2),第一个参数是SYS_name,后面参数照写
#
define
_GNU_SOURCE
#
include
<
stdio.
h>
#
include
<
sys/
types.
h>
#
include
<
pthread.
h>
#
include
<
stdlib.
h>
#
include
<
string
.
h>
#
include
<
unistd.
h>
#
include
<
linux/
unistd.
h>
#
include
<
sys/
syscall.
h>
void
*
thsf(
void
*
arg
)
{
pthread_t
th;
char
*
retstr;
retstr=
malloc
(
50)
;
memset
(
retstr,
0,
50)
;
strcpy
(
retstr,
"New thread quit!"
)
;
th=
pthread_self(
)
;
#
if
1
printf
(
"New thread,pid:%d,tid:%x,another tid:%x./n"
,
getpid(
)
,
th,
(
long
)
syscall(
SYS_gettid)
)
;
#
endif
pthread_exit
(
retstr)
;
}
int
main(
)
{
pthread_t
nth1,
mth,
oth;
void
*
a;
if
(
pthread_create
(
&
nth1,
NULL
,
thsf,
NULL
)
)
printf
(
"Error create new thread./n"
)
;
mth=
pthread_self(
)
;
printf
(
"Main thread,pid:%d,tid:%x,another tid:%x./n"
,
getpid(
)
,
mth,
(
long
)
syscall(
SYS_gettid)
)
;
if
(
!
pthread_join
(
nth1,
&
a)
)
{
printf
(
"New thread quit,message from new thread:%s./n"
,
(
char
*
)
a)
;
free
(
(
char
*
)
a)
;
}
return
0;
}
分享到:
相关推荐
pthread_testcancel pthread_kill pthread_cancel 的使用例子
以下是对pthread_cond_wait的用法进行了详细的分析介绍,需要的朋友可以过来参考下
头文件 #include <pthread.h> 函数原形 pthread_t pthread_self();
编译时报 Looking for pthread_create - not found 的解决办法 linux gcc 编译时报Looking for pthread_create - not found 其解决办法是...
信号pthread_cond_wait信号pthread_cond_wait信号pthread_cond_wait信号pthread_cond_wait信号pthread_cond_wait
一篇博客内容 pthread_cond_wait详解
线程编程——使用pthread_join。 打包文件包含两个文件:c文件源代码、Makefile文件,运行环境在Ubuntu14.04下,使用自带的gcc编译器,同学们只需将文件夹复制到某一目录下之后在终端执行:1.“make”生成“test”可...
为什么在pthread_cond_wait()前要加一个while循环来判断条件是否为假呢?.zip
线程中处理信号pthread_sigmask 的使用
问题原因: pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_atfork()函数建立fork处理程序时,需要链接该库。 问题解决: 在编译...
主要介绍pthread_cread() 的介绍,便于自己学习和回顾。 如有错误欢迎指出。
PThread header file
线程编程——不使用pthread_join。 打包文件包含两个文件:c文件源代码、Makefile文件,运行环境在Ubuntu14.04下,使用自带的gcc编译器,同学们只需将文件夹复制到某一目录下之后在终端执行:1.“make”生成“test”...
pthread资源包,pthread源码和已经编译好的VS2019_x64版本 #include <pthread.h> pthread_t newThread; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS); ...
2.2多线程介绍如果不能从根本上更新当前CPU的架构(在很长一段时间内还不太可能),那么继续提高CPU性能的方法就是超线程CPU模式 2.2.1多线程出现的原因
pthread_kill测试,该函数可用来测试某个进程中的线程执行情况
pthread编程指南,介绍关于PTHREAD的编程,在Linux下的应用。。
32位android中bionic是32位的,其中的mutex只有一半也就是16位能够存储pid,当通过docker运行android时,大概率pid会超过16位的范围,就可能会导致android中mutex死锁,表现为应用卡住黑屏。 [32-bit ABI bugs]...
QT中多线程实例,使用C的pthread_create,传递参数,简单易懂,用工程和可执行程序及源代码,仅供参考