- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道使用不同数量的 pthread_t
变量的差异。
这是我编写的一个简单代码:
#include <stdio.h>
#include <pthread.h>
void *thread(void *vargp);
int main(int argc, char **argv)
{
pthread_t tid;
for (int i = 0; i < 5; i++){
int* a = malloc(sizeof(int));
*a = i;
pthread_create(&tid, NULL, thread, a);
}
if(pthread_join(tid, NULL) == -1){
printf("error");
exit(1);
}
}
void *thread(void *vargp)
{
int count = 1;
for(int i = 0; i <3; i++){
printf("count : %d, value : %d\n", count, (*(int *)vargp));
count++;
}
}
它按照我的意思工作得很好。然而,我觉得奇怪的是,它创建了 5 个线程,但只使用一个 pthread_t 变量!...我看到一些示例,其中使用与它将创建的线程数相同数量的 pthread 变量。例如,如果我要创建如下所示的 5 个线程,则必须创建 5 个长度的 pthread_t
数组,例如 pthread_t tid[5]
。你能告诉我有什么区别吗?谢谢
最佳答案
传递给 pthread_create
的
pthread_t
变量是一个线程标识符,可以根据需要重用。因此,使用相同的标识符 tid
创建多个线程的方式没有任何问题。但是,除了使用它创建的最后一个线程之外,您将丢失所有其他线程的标识,因为 tid
在循环中不断被覆盖。
但是,如果您想从线程接收退出值(使用 pthread_join
)或执行一些其他操作,例如分离线程(使用 pthread_detach
) - - 您可以创建一个处于分离状态的线程,或者线程本身可以将其分离),从您的主线程设置/获取调度参数(使用pthread_setschedparam
,pthread_getschedparam
)等不能这样做,因为您没有线程的 ID。
简而言之,代码非常好,如果您想操作 < 中的线程,则需要存储 ID(例如在示例中使用数组 tid[5]
) em>外部(与线程内部相反——线程可以使用pthread_self()
获取其ID)。
关于c - 仅使用一个 pthread_t 变量和使用多个 pthread_t 变量有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30819452/
如何让 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
我是一名优秀的程序员,十分优秀!