gpt4 book ai didi

c - 第一个线程未使用给定参数运行

转载 作者:行者123 更新时间:2023-11-30 14:57:39 25 4
gpt4 key购买 nike

int run_me(unsigned long prime, unsigned long max, int *ary) {
unsigned long i;
printf("\nI am %d", prime);

if(prime > sqrt(max)) {
return 1; /* do no run */
}

for(i = 3; i*prime < max; i+=2) {
ary[i*prime - 1] = 1;
}

return 0;
}

typedef struct Args {
unsigned long max, prime;
int *ary;
} args;

void *thread_runner(void *all_args) {
args *my_args = all_args;
run_me(my_args->prime, my_args->max, my_args->ary);
return 0;
}

unsigned long *sieve_of_eratosthenes(unsigned long begin, unsigned long end) {
unsigned long i, j, arylen, *ary_to_ret;
unsigned long current_primes[4] = {3, 5, 7, 11}; /* holds primes being used by threads*/
int *ary_of_all;
pthread_t threads[4];
args *curr;

curr = malloc(sizeof(args));

ary_of_all = calloc(end, sizeof(int));
arylen = end - begin + 2;
ary_to_ret = calloc(arylen, sizeof(unsigned long));
ary_of_all[0] = 1;

/*mark all even numbers*/
for(i = 1; 2 * i < end; i++) {
ary_of_all[2*i - 1] = 1;
}

while(current_primes[3] < sqrt(end)) {
/*run threads with current primes*/
for(i = 0; i < 4; i++) {
curr->prime = current_primes[i];
curr->max = end;
curr->ary = ary_of_all;
pthread_create(&threads[i], NULL, thread_runner, curr);
}
/* join all threads */
for(i = 0; i < 4; i++) {
pthread_join(threads[i], NULL);
}

j = 0; /* number of primes found */

/*find new primes*/
for(i = current_primes[3] + 2; i < end && j < 4; i+=2) {
if(ary_of_all[i - 1] == 0) {
current_primes[j] = i;
j++;
}
}

}

/*run threads one more time*/
if(current_primes[0] <= sqrt(end)) {
for(i = 0; i < 4; i++) {
curr->prime = current_primes[i];
curr->max = end;
curr->ary = ary_of_all;
pthread_create(&threads[i], NULL, thread_runner, curr);
}
/* join all threads */
for(i = 0; i < 4; i++) {
pthread_join(threads[i], NULL);
}
}

/*create the array to be returned*/
j = 0; /*pos in *ary_to_ret*/
for(i = begin; i <= end; i++) {
if(ary_of_all[i-1] == 0) {
ary_to_ret[j] = i;
j++;
}

}

ary_to_ret[j] = 0; /* null terminate */
ary_to_ret = realloc(ary_to_ret, (j+1) * sizeof(unsigned long));
return ary_to_ret;
}

我运行上面的代码是为了使用埃拉托斯特尼筛法获得给定高值和低值的素数列表。我的代码大部分工作正常,但是当我运行此代码时,我使用 curr_primes 数组中的第一个元素创建的线程从未使用过,而是运行 5、7、11、11。每次运行该数组时都会执行此操作并重新填充它。我想知道是否有人可以向我解释为什么它以这种方式运行。

最佳答案

您将相同的 curr 指针传递给所有线程。你很幸运,它的工作效果与你观察到的一样好,因为这是一个巨大的竞争条件。相反,代码需要将单独的参数缓冲区传递给每个线程。这是一个例子:

/* doesn't really need to be dynamic memory in this simple example */
args curr[4];

for(i = 0; i < 4; i++) {
curr[i].prime = current_primes[i];
curr[i].max = end;
curr[i].ary = ary_of_all;
pthread_create(&threads[i], NULL, thread_runner, &curr[i]);
}

/* join all threads */
for(i = 0; i < 4; i++) {
pthread_join(threads[i], NULL);
}

关于c - 第一个线程未使用给定参数运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43771254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com