- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这里是多线程初学者。恰好在第 5 次迭代(即执行 pthread_join(threadID[4], NULL) 时),我的程序因段错误而失败。
我正在创建多个线程来从计数器变量中加/减 1 以研究竞争条件。一切顺利,直到我尝试 5 个或更多线程。在 pthread_join(threadID[4], NULL) 的最后一次迭代中,它失败了,我无法确定原因。我确信问题就在那里,因为我使用 printf 语句来查看它在失败之前到达的位置。
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include <time.h>
int opt_threads;
int opt_iterations;
long nThreads;
long nIterations;
int opt_yield;
long long counter;
void add(long long *pointer, long long value) {
long long sum = *pointer + value;
if (opt_yield)
sched_yield();
*pointer = sum;
}
void *thread_worker(void * arg) {
long i;
for (i=0; i<nIterations; i++) {
add(&counter, 1);
add(&counter, -1);
}
return arg;
}
int main(int argc, char *argv[]) {
int c;
pthread_t *threadID = malloc(nThreads * sizeof(pthread_t));
if (threadID == NULL) {
fprintf(stderr, "Thread memory allocation failed\n");
exit(1);
}
static struct option long_options[] =
{
{"threads", required_argument, 0, 't'},
{"iterations", required_argument, 0, 'i'},
{"yield", no_argument, 0, 'y'},
{0,0,0,0}
};
while (1) {
c = getopt_long(argc, argv, "", long_options, NULL);
if (c==-1) break;
switch(c) {
case 't':
opt_threads = 1;
nThreads = atoi(optarg);
break;
case 'i':
opt_iterations = 1;
nIterations = atoi(optarg);
break;
case 'y':
opt_yield = 1;
break;
default:
fprintf(stderr, "Bad argument!\n");
exit(1);
}
}
counter = 0;
struct timespec start, finish;
int i;
//start clock
clock_gettime(CLOCK_MONOTONIC, &start);
//create
for (i=0; i < nThreads; i++) {
pthread_create(&threadID[i], NULL, &thread_worker, NULL);
printf("Created thread[%ld]\n", i);
}
//wait (join)
/*for (i=0; i < nThreads; i++) {
printf("Now i is %ld\n", i);
if (pthread_join(threadID[i], NULL) != 0)
fprintf(stdout,"ERRRRROOOORRRRRRRR\n");
}*/
pthread_join(threadID[0], NULL);
pthread_join(threadID[1], NULL);
pthread_join(threadID[2], NULL);
pthread_join(threadID[3], NULL);
pthread_join(threadID[4], NULL);
printf("about to end clock\n");
//finish clock
clock_gettime(CLOCK_MONOTONIC, &finish);
printf("finished clock\n");
long seconds = finish.tv_sec - start.tv_sec;
long ns = finish.tv_nsec - start.tv_nsec;
long runTime = (seconds + ns) * 1000000000L;
long nOperations = nThreads * nIterations * 2;
long avgOperations = runTime / nOperations;
long run_time = 1000000000L * (finish.tv_sec - start.tv_sec) + finish.tv_nsec - start.tv_nsec;
//Print
if (opt_yield == 0)
fprintf(stdout, "add-none, %ld, %ld, %lld, %ld, %lld, %lld\n", nThreads, nIterations, nOperations, run_time, run_time/nOperations, counter);
else if (opt_yield == 1)
fprintf(stdout, "add-yield-none, %ld, %ld, %lld, %ld, %lld, %lld\n",nThreads, nIterations, nOperations, run_time, run_time/nOperations, counter);
exit(0);
}
我希望程序能够正确等待第 5 个线程,但由于段错误而失败。
最佳答案
您的 main
函数开始:
int main(int argc, char *argv[])
{
int c;
pthread_t *threadID = malloc(nThreads * sizeof(pthread_t));
因为 nThreads
是一个没有显式初始化器的全局变量,它的值为零——您还没有分配任何可以合法使用的内存。访问该内存会导致未定义的行为。
将内存分配推迟到您知道需要多少线程之后。
未定义的行为意味着任何事情都可能发生,包括看似有效直到它不再有效。
您还需要将线程加入代码重写为一个循环,以匹配创建线程的循环。
关于c - 为什么 pthread_join 在最后一次迭代中失败(给出段错误)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57350633/
如果用户在命令行中仅输入 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],它包含最
我是一名优秀的程序员,十分优秀!