这是我使用4个线程的质数计算器的c实现,它仅在2个质数之后停止。我真的很感谢能帮助它正常运行以及能帮助它更快地工作的任何帮助。先感谢您

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <assert.h>
#include <pthread.h>
#include <limits.h>

unsigned long *ary;
unsigned long max;


struct arg_struct{
  unigned long start;
  unsigned long end;
};


void * thread_do(void * input){
  unsigned long i, j;
  struct arg_struct * param = input;
  for (i = 3; i <= sqrt(max) + 1; i += 2) {
    for (j = param->start; j < param->end; j++) {
      if (ary[j] && ary[j] % i == 0 && ary[j] != i) {
    ary[j] = 0;
      }
    }
  }

  return NULL;
}

unsigned long *sieve_of_eratosthenes(unsigned long begin, unsigned long end){
  unsigned long i, j = 0, interval, arylen;
  pthread_t *threads = malloc(sizeof(pthread_t *) * 4);
  void *thread_return_value;
  max = end;

  struct arg_struct *thread1 = malloc(sizeof(struct arg_struct));;
  struct arg_struct *thread2 = malloc(sizeof(struct arg_struct));;
  struct arg_struct *thread3 = malloc(sizeof(struct arg_struct));;
  struct arg_struct *thread4 = malloc(sizeof(struct arg_struct));;

  /* skip initial even or initial 1 */
  while (begin == 1 || begin % 2 == 0)
    begin += 1;

  /* possible that begin==end==5; result array should be 5, 0 */
  arylen = end - begin + 2;

  ary = calloc(arylen + 1, sizeof(unsigned long));
  if (!ary)
    exit(1);

  /* setup the odds-only array. */
  for (i = begin; i <= end; i += 2) {
    ary[(i - begin) / 2] = i;
  }

  interval = end - begin;
  thread1->start = begin;
  thread1->end = begin + interval/4;

  thread2->start = begin + interval/4 + 1;
  thread2->end = begin + interval/2;

  thread3->start = begin + interval/2 + 1;
  thread3->end = begin + (3 * interval)/4;

  thread4->start = begin + (3 * interval)/4 + 1;
  thread4->end = end;

  /* clear non-primes with threads*/
  pthread_create(&threads[0], NULL, thread_do, thread1);
  pthread_create(&threads[1], NULL, thread_do, thread2);
  pthread_create(&threads[2], NULL, thread_do, thread3);
  pthread_create(&threads[3], NULL, thread_do, thread4);


  j = 0;
  for (i = 0; i < arylen && j < arylen; i++) {
    for (; j < arylen && ary[j] == 0; j++);
      if (j < arylen) {
        assert(ary[j] != 0);
        assert(i <= j);
        ary[i] = ary[j];
      } else {
        break;
      }
      j++;
    }

  for(i = 0; i < 4 ; i++){
    pthread_join(threads[i], &thread_return_value);
  }


  ary[i] = 0;
  ary = realloc(ary, (i + 1) * sizeof(unsigned long));

  return ary;
}

最佳答案

GCC(Gnu编译器集合)返回以下错误。

h.c:14:3: error: expected specifier-qualifier-list before ‘unigned’
   unigned long start;
   ^
h.c: In function ‘thread_do’:
h.c:23:19: error: ‘struct arg_struct’ has no member named ‘start’
     for (j = param->start; j < param->end; j++) {
                   ^
h.c:23:37: error: ‘struct arg_struct’ has no member named ‘end’
     for (j = param->start; j < param->end; j++) {
                                     ^
h.c: In function ‘sieve_of_eratosthenes’:
h.c:61:10: error: ‘struct arg_struct’ has no member named ‘start’
   thread1->start = begin;
          ^
h.c:62:10: error: ‘struct arg_struct’ has no member named ‘end’
   thread1->end = begin + interval/4;
          ^
h.c:64:10: error: ‘struct arg_struct’ has no member named ‘start’
   thread2->start = begin + interval/4 + 1;
          ^
h.c:65:10: error: ‘struct arg_struct’ has no member named ‘end’
   thread2->end = begin + interval/2;
          ^
h.c:67:10: error: ‘struct arg_struct’ has no member named ‘start’
   thread3->start = begin + interval/2 + 1;
          ^
h.c:68:10: error: ‘struct arg_struct’ has no member named ‘end’
   thread3->end = begin + (3 * interval)/4;
          ^
h.c:70:10: error: ‘struct arg_struct’ has no member named ‘start’
   thread4->start = begin + (3 * interval)/4 + 1;
          ^
h.c:71:10: error: ‘struct arg_struct’ has no member named ‘end’
   thread4->end = end;
          ^


来自内存分配问题的主要错误之一是,
是您使用的是未标记的,而不是未签名的:)。

关于c - 我的代码仅计算序列中的前两个素数并停止,因此我无法弄清楚为什么在计算其余数之前会停止,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20479056/

10-17 00:24