- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是《Unix环境高级编程》中创建线程的示例代码。关于线程的创建,err = pthread_create(&ntid, NULL, thr_fn, NULL);
新创建的线程是否可以只使用ntid打印自己的threadID,而不是调用pthread_self()?
#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: "); /* for the newly created thread,can it
* print its own threadID just using ntid,
* instead of calling pthread_self()
*/
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);
}
最佳答案
让我们检查规范:
http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_create.html
pthread_create(pthread_t *restrict thread,
Upon successful completion, pthread_create() shall store the ID of the created thread in the location referenced by thread.
The calling thread can obtain the ID of the created thread through the return value of the pthread_create() function, and the newly created thread can obtain its ID by a call to pthread_self().
问题是:ntid
是调用函数的变量;即使它是全局的,从线程读取这个全局变量也不会扩展到两个或更多创建的线程。
另一个问题是执行的时间和顺序。 pthread_create 将线程 id 写入 ntid
after 创建新线程:
Upon successful completion, pthread_create() shall store the ID of the created thread in the location referenced by thread.
因此,在没有同步的情况下从创建的线程读取 ntid
是不安全的,新线程可能会在将实际值写入全局之前读取。
所以,你应该使用pthread_self
:http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_self.html
The pthread_self() function shall return the thread ID of the calling thread.
The pthread_self() function provides a capability similar to the getpid() function for processes and the rationale is the same: the creation call does not provide the thread ID to the created thread.
关于linux - 为什么新创建的线程不是通过pthread_create()返回参数获取它的tid,而是pthread_self(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38136384/
这个 MySQL 查询有其他选择吗? SELECT * FROM type WHERE tid='1' OR tid='2'; 这里的 type 是表格,tid 是表格的 ID,我想以任何方式选择前
我有这个通用类,它使用 Entity Framework 6.x。 public class GenericRepository where TEntity, class, IIdentifyable
我正在使用 glib 创建线程: g_thread_create() 如何获取线程id(编号)? 最佳答案 你不能。线程 ID 在 GLib 中被抽象出来。你需要它做什么? 关于glib - glib
我正在使用 std::thread 来启动线程。另外,我需要 /proc/[pid]/tasks/[tid] 中提供的工作线程的统计信息。我需要 tid 才能监视线程统计信息。我想知道是否有办法从父线
http://www.cs.colostate.edu/~cs370/Spring15/Workbook/thread_ex.txt 我的教授提供了上面的示例代码(没有复制以保护他的 IP),我对输出
在 Linux ps -eLf | grep my-process-name给出了我的进程中的线程列表以及每个线程的 TID。 在 OSX 上 ps -M pid给了我线程列表,但没有显示每个线程的
当 TID 响应 kill 系统调用时,有什么方法可以检测到 PID 真的死了,然后就像进程仍然存在一样。 #include #include #include #include #inclu
我创建了一个多线程模拟器,它严重依赖于线程之间传递的 native 消息(不要告诉我去单线程它是为了我关于 D 的论文,我需要让它工作) 在一次非常耐用的杂物之后,涉及到和来自 shared 的大量物
我发现在下面的代码中,每次接受新客户端时都会创建一个线程。在函数 pthread_client() 中没有退出日志。但是似乎没有通过命令 ps aux 创建多线程 我的理解是,每次创建一个具有相同ti
我一直在尝试使用 tgkill 远程终止一个线程。我知道pthread_kill 被推荐用于这种事情,因为没有任何 glibctgkill 的包装器,但是,我将在进程之外进行 kill 调用。 我在这
我正在为应用程序使用 Visualforce Remoting,并定期收到错误: Uncaught TypeError: Cannot read property 'tid' of undefined
我想在Asus_Z008D上构建我的应用程序,但是我有这个问题: [Mono] Could not load file or assembly 'System.Runtime, Version=4.0
我只是遇到了问题。当我将gradle版本更改为2.1.0(旧版本是1.5.0)时,我的代码出错了。而且它没有logcat,所以我只是不知道如何查找问题和错误的零件代码。我试图在我的AndroidMan
我调用内核函数 sched_setaffinity() 以尝试使用 CPU 锁定内核线程,但它总是失败,错误号 22 显示“参数无效”。该机器运行 Fedora 15,内核版本为 2.6.38。 如果
当我记录名为 tid 的键的 json 字符串时,它总是返回 0。请检查以下示例。 var transaction = {tid:1, type:0, time:126312736}; var tra
JVMTI 有一个函数叫做GetAllThreads。但是我们只能得到jthread对象。有什么方法可以获取 jthread 对象的 tid 吗?我所说的 tid 是指由 gettid() 系统调用返
这个问题不是关于 pthread ID,而是关于通常由 gettid 或 ptrace 获取的值。 linux 内核是否提供任何机制以相对有效的方式从给定的 tid 中获取线程组 ID(通常通过 ge
我使用以下内容来获取 drupal 8 中分类术语的父级: $parent = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->l
我的应用程序崩溃,除了出现此错误外,没有任何堆栈跟踪: A/libc: Fatal signal 11 (SIGSEGV), code 128, fault addr 0x0 in tid 12680
我必须这样做: 编写一个程序,其主线程创建 3 个其他线程。这些线程(不同于主线程)中的每一个都应写入其 pid 和 tid,并终止返回 1 到 3 之间的整数,并且不同于其他线程返回的值。主线程应在
我是一名优秀的程序员,十分优秀!