- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
该引用来自 man page of pthread_self() .
那么,我应该根据什么来决定我应该使用 pthread_self
还是 gettid
来确定哪个线程正在运行该函数?
两者都是不可移植的。
为什么有两个不同的函数来获取线程ID?
最佳答案
So, on what basis should I decide whether I should use pthread_self or gettid to determine which thread is running the function?
只要您想在应用程序中识别线程,就应该始终使用 pthread_self()
。 gettid()
可以用于某些目的并且如果您知道它是 Linux。例如,gettid()
可用于获取线程特定种子的种子(在 srand()
中使用)。
Both are non portable.
这并不完全正确。 gettid()
不可移植,因为它是 Linux 特定的函数。但是 pthread_self()
是可移植的,只要您不对其表示做任何假设。
例如,以下是不可可移植的。
printf("Thread ID is: %ld", (long) pthread_self());
因为不能保证任何 pthread_self()
都会是某种整数。但是
pthread_t my_tid; //filled elsewhere
pthread_t tid = pthread_self();
if( pthread_equal(my_tid, tid) ) {
/* do stuff */
}
完全便携。
前者不可移植,因为它假设线程 ID 是整数,而后者不是。
Why are there two different functions to get the thread ID?
它们不是获得相同值(value)的两种不同方式。一个 (pthread_self()
由线程库 (pthreads) 提供,而另一个 (gettid()
是操作系统特定的函数。不同的操作系统可能提供不同的接口(interface)/syscall 获取类似于 gettid()
的线程 ID。因此您不能在可移植应用程序中依赖 gettid()
。
关于c - pthread_self() 返回的线程 ID 与调用 gettid(2) 返回的内核线程 ID 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34370172/
我正在使用 C 学习线程,并发现了以下“Hello World”程序。当我使用 pthread_join() 在线程内和线程外打印 pthread_value() 的值时,两个线程返回的值完全不同。
我学pthread的时候,手册上说pthread_self()总是成功 ERRORS This function always succeeds. 这是怎么发生的?我们如何确定一个函数是否总是成功?
pthread_self() 在 ubuntu 12.04 上是否昂贵? 它是否使用系统调用? 我想在每次线程写入日志时调用 pthread_self(),这样我就知道哪个线程写入日志并在日志中提及它
我想通过我们可以传递给 pthread_self() 的某个函数来获取线程的堆栈地址。是否可以?我这样做的原因是因为我想为它的堆栈中某处的线程编写我自己分配的线程标识符。我可以在堆栈末尾附近写入(堆栈
我有以下小代码片段: int main() { pthread_t t = pthread_self(); std::cout g_thread_ids构造并链接来自 pthread_sel
#include #include #include #include int main(){ int systid=syscall(186); int pt_tid=pthread_
这就是我想知道的: 如果我使用 pthread_create() 创建一个线程,然后从该线程调用 pthread_self(),该值是否与我在主线程中传递给 pthread_create 的 pthr
我正在尝试用 C 实现 pthread_self() 但我对它到底做了什么感到困惑。我知道它返回线程 ID,但该 ID 是一个内存位置,因为它返回一个我不确定如何解释的 pthread_t 。另外,我
我无法让 pthread_equal() 匹配存储的 pthread。与从 pthread_create() 获得的相比,我从 pthread_self() 获得的 pthread_t 似乎被截断了
我正在查看这个示例代码 http://man7.org/linux/man-pages/man3/pthread_setaffinity_np.3.html 在这个例子中,为什么可以在没有 pthre
根据我的理解,gettid()返回的TID(thread id)在一个进程中(或者在一个多进程的程序中,而每个进程可能有多个线程),即在一个进程内,不同的线程有不同的线程id。 pthread_sel
我正在尝试在 for 循环中创建多个线程(代表人员),并显示作为参数传递的人员 ID 以及线程 ID。人员 ID 按预期显示,但线程 ID 始终相同。 #include #include #inc
我正在尝试将 linux 工作应用程序移植到 windows(使用 windows.h 和 POSIX pthread_win32) 因此,我定义了一个客户字段: struct ClientIdent
在 Glibc 的 pthread.h 中,pthread_self 函数是用 const 属性声明的: extern pthread_t pthread_self (void) __THROW __
我正在尝试在 Linux 上设置线程的 CPU 关联。我想知道推荐使用以下哪一种方法: Get thread id using pthread_self() Set CPU affinity usin
背景:我正在开发一个被许多程序使用的日志库。 我正在为每个线程分配一个人类可读的名称,主线程应该是“main”,但我希望能够从库中检测到该状态,而不需要在每个 main() 函数的开头编写代码. 另请
#include #include int main(){ std::cout<
我正在按照“Android NDK 游戏开发指南”中的示例创建跨平台线程包装器,以便在我自己的 Android NDK 游戏引擎中使用。在示例的 Thread 类中,在某个时刻,会检查线程句柄是否对应
我的问题是是否有一个函数返回 pthread_self 和 gettid 以外的线程 ID。 pthread_self 的问题在于它返回一个地址,而 gettid 返回系统范围的全局 tid。我想要相
我正在使用来自 here 的这个 dtrace 脚本尝试查找 Java 程序的线程何时发生上下文切换。 我正在尝试将从脚本中收集的数据与从正在运行的程序中收集的跟踪数据(例如方法进入/退出之类的东西)
我是一名优秀的程序员,十分优秀!