- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用 POSIX pthread 库编写了以下代码:
#include <stdlib.h>
#include <pthread.h>
void *thread_function(void *arg) {
char *code = "1";
}
int main() {
int res;
pthread_t a_thread;
void *thread_result;
res = pthread_create(&a_thread, NULL, thread_function, NULL);
if (res != 0) {
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
sleep(5);
printf("\nWaiting for thread to finish...\n");
res = pthread_join(a_thread, &thread_result);
printf("res[%d]\n",res);
if (res != 0) {
perror("Thread join failed");
exit(EXIT_FAILURE);
}
res = pthread_join(a_thread, &thread_result);
printf("res[%d]\n",res);
exit(EXIT_SUCCESS);
}
执行代码时,我得到以下输出:
Waiting for thread to finish...
res[0]
Segmentation fault (core dumped)
在代码中,我想测试如果调用 pthread_jion() 函数会发生什么线程完成后两次。第一次调用该函数是正确的,第二次则崩溃。回溯:
Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 pthread_join (threadid=140150565050112, thread_return=0x7fffa0a2c508) at
pthread_join.c:47
47 if (INVALID_NOT_TERMINATED_TD_P (pd))
(gdb) bt
Python Exception exceptions.ImportError No module named gdb.frames:
#0 pthread_join (threadid=140150565050112, thread_return=0x7fffa0a2c508) at
pthread_join.c:47
#1 0x00000000004008d5 in main ()
我检查了 pthread_join.c 文件:
39 int
40 pthread_join (threadid, thread_return)
41 pthread_t threadid;
42 void **thread_return;
43 {
44 struct pthread *pd = (struct pthread *) threadid;
45
46 /* Make sure the descriptor is valid. */
47 if (INVALID_NOT_TERMINATED_TD_P (pd))
48 /* Not a valid thread handle. */
49 return ESRCH;
第 47 行,宏定义检查 pd 是否是有效的线程句柄。如果不是,则返回ESRCH(3)。
但是,当我在另一个 Linux 环境中运行相同的代码时,我得到以下输出:
Waiting for thread to finish...
res[0]
res[3]
这和环境有关系吗?两个Linux系统具有相同的ldd版本:
ldd (GNU libc) 2.17
相同的 GLIBC:
GLIBCXX_3.4
GLIBCXX_3.4.1
GLIBCXX_3.4.2
GLIBCXX_3.4.3
GLIBCXX_3.4.4
GLIBCXX_3.4.5
GLIBCXX_3.4.6
GLIBCXX_3.4.7
GLIBCXX_3.4.8
GLIBCXX_3.4.9
GLIBCXX_3.4.10
GLIBCXX_3.4.11
GLIBCXX_3.4.12
GLIBCXX_3.4.13
GLIBCXX_3.4.14
GLIBCXX_3.4.15
GLIBCXX_3.4.16
GLIBCXX_3.4.17
GLIBCXX_3.4.18
GLIBCXX_3.4.19
GLIBCXX_3.4.20
GLIBC_2.3
GLIBC_2.2.5
GLIBC_2.14
GLIBC_2.17
GLIBC_2.3.2
GLIBCXX_FORCE_NEW
GLIBCXX_DEBUG_MESSAGE_LENGT
和相同的linux内核版本:
Red Hat Enterprise Linux Server release 6.6 (Santiago)
最佳答案
pthread_join
在线程终止后调用pthread_detach
。pthread_detach
当线程终止时释放所有线程资源。
摘自pthread_detach
的文档
Attempting to detach an already detached thread results in unspecified behavior.
所以你有未指定的行为,因此你无法保证之后会发生什么。
至少,threadid
指向的内存将被释放,从而导致访问已释放的内存。
简而言之,不要对同一个 threadid 调用 pthread_join
两次。你为什么要这么做?
编辑:更简单:pthread_join
的手册页说:
Joining with a thread that has previously been joined results in undefined behavior.
关于c++ - 两次调用 pthread_join() 时 glibc pthread_join 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45703490/
如果用户在命令行中仅输入 1 个数字,则该程序可以正常工作。它会分解出主要因素并将它们输出到控制台。 J_10542741@cs3060:~/assn3$ ./assn3 12 12: 2, 2, 3
我使用 POSIX pthread 库编写了以下代码: #include #include void *thread_function(void *arg) { char *code =
当我使用 pthread_join() 时,我不确定它是否位于正确的位置。就像现在一样,它会等待线程退出然后再次迭代循环吗?我想我要问的是我应该将其从双 for 循环中取出并在 pthread_joi
#define SIZE 3 #define MAX_THREADS 9 int main() { ... pthread_t m_threads[MAX_THREADS];
来自https://computing.llnl.gov/tutorials/pthreads/ : A joining thread can match one pthread_join() cal
我找到了这个 Sockets 教程 http://www.binarytides.com/socket-programming-c-linux-tutorial/我在最后一个例子中遇到了麻烦。它是一个
因此,当我运行代码时,我在 pthread_join 处遇到段错误。我的 pthread_join 之后有一条打印语句未运行。有谁知道为什么吗?你能给我一些关于如何解决这个问题的提示或想法吗? 输出打
我一直在使用 pthreads,我使用 for 循环和 ProducerThread[index] 和 conssumerThread[index] 将它们创建到一个数组中,从 0 到用户想要的任意数
我的程序需要创建一些线程,但我被困在 pthread_join 上,因为它总是进入错误情况,因为返回(安全)是 3,而不是 0,我认为这是正确的数字,以防万一一切顺利。 编辑:为了更好地解释自己,错误
我正在使用 C 语言的 pthreads 工作,并且再次遇到了问题。我正在尝试将结果作为函数数组发送到我的主线程。 此代码查找每个工作人员的最大值(1 个工作人员/行)并保存该值的索引。到目前为止,一
不知何故,我的代码返回了段错误。 sockfd 是一个 int 或文件描述符 pthread_t tid[4]; int err; int k = 0; while (k < 4) { err
我对此有疑问,因为在 pthread_join 中获得响应时,程序出现段错误,我不知道如何解决此问题。 这是我的代码: #include #include #include struct mat
我正在研究这个问题,但我不理解其中的一部分。该脚本不是英文的,因此翻译会非常乏味,但基本问题是让线程读取特定的文本文件并找到特定的单词。每个文件都有自己的线程等等。最后两个问题是确保同一文件中出现的各
我在 Windows 上使用 pthreads.h 作为简单的光线跟踪器。主函数似乎没有等待线程完成。当我像这样运行程序时(我现在简化了它,只是测试线程,但它仍然给出错误): typedef stru
你能解释一下为什么下面的 pthread_join 不起作用吗?它会阻塞我的代码。如果我评论这三行,我的代码会执行预期的操作,但显然我不知道线程是否终止(在我的代码中没有问题,但在更大的情况下有问题)
我的代码遇到了问题。以下代码启动 n 个线程,这些线程竞争寻找 n 个不同矩阵的每个对角线的最大值。 #include #include #include #include #include
下面的代码对于第二种情况给出了段错误,但对于第一部分它工作正常。但他们俩都在做同样的事情。这里 pthread_join() 调用不会生成任何错误,但是在打印 pthread_join() 的响应时,
int main() { pthread_t thread; int a = 0; while(a == 0) { accept(...); //acc
以下程序处理nbFiles每个 GROUPSIZE 使用 1 个工作线程的文件文件。不超过MAXNBRTHREADS工作线程是并行运行的。一个watchDog()线程(线程 0)用于管理 PTHREA
我利用 pthreads 构建了这段代码。目标是构建一个数组 X[N][D] 并为其分配随机值。您可以将该数组的元素读取为某些点的系数。 下一步,我尝试计算一个数组 distances[N],它包含最
我是一名优秀的程序员,十分优秀!