- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我想为给定的线程 ID 调用 pthread_join,但前提是该线程已启动。安全的解决方案可能是添加一个变量来跟踪哪个线程在哪里启动。但是,我想知道是否可以检查 pthread_t 变量,类似于以下代码。
pthread_t thr1 = some_invalid_value; //0 ?
pthread_t thr2 = some_invalid_value;
/* thread 1 and 2 are strated or not depending on various condition */
....
/* cleanup */
if(thr1 != some_invalid_value)
pthread_join(&thr1);
if(thr2 != some_invalid_value)
pthread_join(&thr2);
其中 some_invalid_value 可以是 0,或依赖于实现的“PTHREAD_INVALID_ID”宏
PS:我的假设是 pthread_t 类型具有可比性和可分配性,假设基于
PPS:我想这样做,因为我认为在无效线程 id 上调用 pthread_join 是未定义的行为。它不是。但是,加入先前加入的线程是未定义的行为。现在让我们假设上面的“函数”被重复调用。无条件调用 pthread_join 并检查结果可能会导致在先前加入的线程上调用 pthread_join。
最佳答案
你的假设一开始就不正确。 pthread_t 对象是不透明的。您不能直接在 C 中比较 pthread_t 类型。您应该改用 pthread_equal。
另一个考虑是如果 pthread_create 失败,你的 pthread_t 的内容将是未定义的。它可能不再设置为您的无效值。
我的偏好是保留 pthread_create 调用的返回值(连同线程 ID),并使用它来确定每个线程是否正确启动。
关于linux - 是否存在无效的 pthread_t id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6276939/
如何让 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
我是一名优秀的程序员,十分优秀!