- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
假设我有代码片段:
...
pthread_t tt;
...
pthread_create(&tt, NULL, thread_func, NULL);
...
if( tt != 0)
{
pthread_join(tt, NULL);
tt = 0;
}
问题:tt可读吗?可写?当我四处搜索时,没有发现这方面的明确规范,而且 pthread_t 是不透明的。谢谢和问候,
最佳答案
pthread_t
当然是可读可写的。问题是您不知道向它写入什么(或如何写入)或如何从中读取。您可以将整个事物作为不透明单元传递或获取其地址,或将其传递给多个 pthread_xxx
函数,但除此之外,pthread_t
值是不透明且未指定的,因此您无法可移植地为其赋值或对其当前值做出任何决定。
例如,在 Linux 中,tt = 0;
赋值是安全的,但在不同的实现中可能并非如此。即使安全,也不能保证零值是合法的“无效”值,您可以使用它来表示线程不存在。
pthread_self(3)
的 linux 手册页包括以下文本:
POSIX.1 allows an implementation wide freedom in choosing the type used to represent a thread ID; for example, representation using either an arithmetic type or a structure is permitted. Therefore, variables of type
pthread_t
can't portably be compared using the C equality operator (==); usepthread_equal(3)
instead.Thread identifiers should be considered opaque: any attempt to use a thread ID other than in pthreads calls is nonportable and can lead to unspecified results.
因此唯一可移植的方法是为每个 pthread_t
保留一个单独的标志,以表明它是否(或仍然)存在。
关于C pthread_t 读/写允许吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53163420/
如何让 pthread_t 停止一段时间?我的意思是不使用 sleep 功能;) 最佳答案 使用pthread_cond_wait(或pthread_cond_timedwait,但可能不是)阻塞线程
我想知道使用不同数量的 pthread_t 变量的差异。 这是我编写的一个简单代码: #include #include void *thread(void *vargp); int main(i
我想像这样将 pthread_t 存储在数组中: pthread_t tThreads[nThreads]; 不久之后,我使用 for 循环遍历数组以启动线程 pthread_create( &tTh
我写这个简短的例子是为了理解 C 语言的线程编程。它应该写“线程 0”。但没有输出。这是代码。 #include #include #include int i=0; pthread_mutex
我真正需要的是 *nix pthread 标识符 (pthread_t) 的可移植比较和打印。存在 pthread_equal 函数来比较两个线程 id 是否相等,但是不可能将它们与运算符 => (当
我写了一个小的 pthread 多线程编程,我想清理我创建的 pthred_t 数组。但是当我这样做时,我得到了一个 glib 已被检测到的错误。我怎样才能删除数组,或者我根本不需要。代码在没有释放内
我有一个接受输入的程序 n从命令行,并作为其操作的一部分,尝试调用以下代码: pthread_t threads[n*n]; 现在,对于任何n 1023 时,我遇到了段错误。正是这条特殊的线导致了它
我正在使用 pthread_t 打印出我在 C 中手动创建的线程的 pid。但是,我在创建新线程之前打印它(通过 ref 作为参数传递)并且它打印出不同的值(大概我的主要功能正在执行的线程)。我本来希
我正在使用 C 和 pthreads 进行编程。我有一个长时间运行的函数,我想在一个单独的线程中运行: void long_running_function(void * arg) { ...
已搜索,但没有找到令人满意的答案。 我知道没有可移植的方式来打印 pthread_t。 你是如何在你的应用中做到这一点的? 更新: 实际上我不需要 pthread_t,而是一些小的数字 id,在调试消
我有一个 C 头文件,它定义了以下函数: void my_func(pthread_t tid); 这是由另一个函数定义的: void my_func(pthread_t tid) { ... 当我编
我编写此测试代码是为了尝试将线程2的pthread_t传递给线程1,并编写了让主线程等待thread1完成的代码,而不是thread1 等待thread2 完成: void *function_
我现在尝试通过两个线程(主线程和第二个线程)之间的管道发送一些数据,但我收到有关文件描述符的 errno 9。我认为当涉及线程时文件描述符是重复的,但这里似乎不是这种情况。它通常会返回“read fr
我想为给定的线程 ID 调用 pthread_join,但前提是该线程已经启动。安全的解决方案可能是添加一个变量来跟踪哪个线程已启动或未启动。但是,我想知道是否可以检查 pthread_t 变量,类似
我想在映射中存储我自己的一个类,键是 pthread_t 类型。(我类的每个对象都包含一个 pthread_t 数据成员,所以我希望每个实例都与该 pthread_t 作为键相关联, 在 map 中)
我有一组 pthreads,它们都需要完成一个任务,然后阻塞直到最后一个完成任务,然后让它们都转到下一个任务。许多任务都会发生这种情况。 我正在考虑有一个计数器,它在每个任务开始时开始等于线程数,并且
是否可以确定我声明的 pthread_t 结构是否未使用?我有一个 pthread_t 结构数组,在我的程序结束时,我对每个结构调用 pthread_join(),但如果其中一些从未使用过,我不想对它
假设我有代码片段: ... pthread_t tt; ... pthread_create(&tt, NULL, thread_func, NULL); ... if( tt != 0) {
SO 上的帖子表明 pthread_t 是不透明类型,不是数字,当然也不是线程索引,你不应该直接比较 pthread_t 的,等等等等 问题: 为什么?是否真的有意支持没有线程数字 ID 的系统?当
我正在尝试动态创建 pthread 并面临变量寻址问题。你能告诉我应该如何访问地址吗 int main (int argc, char *argv[]) { pthread_t *threads
我是一名优秀的程序员,十分优秀!