0

我试图通过将数组拆分为大小为 10 的段,将 1000 个元素的整数数组(其中每个元素为 1)与 pthread 库相加。因此,有效地使用了 100 个线程来执行此操作。这个并行操作的结果和预期的一样(1000)。但有趣的是,我在创建线程之前计算的顺序总和在我第一次调用pthread_join(). 不确定我是否在这里遗漏了什么。有人可以在这里发现错误吗?

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define SEGMENT_SIZE 10
#define NUM_THREADS 100
int *array = NULL;

void* segment_sum(void *args)
{
        int index = (int)args;
        int sum = 0;

        for (int i = index * SEGMENT_SIZE; i < (index + 1) * SEGMENT_SIZE; i++) {
                sum += array[i];
        }

        return (void *)sum;
}

int main()
{
        pthread_t thread[NUM_THREADS];
        int res = 0;
        int seq_res = 0;
        int par_res = 0;

        array = calloc(1, sizeof(int) * NUM_THREADS * SEGMENT_SIZE);
        for (int i = 0; i < NUM_THREADS * SEGMENT_SIZE; i++) {
                array[i] = 1;
                seq_res += 1;
        }


        for (int i = 0; i < NUM_THREADS; i++) {
                res = pthread_create(&thread[i], NULL, segment_sum, (void *)i);
                if (res != 0) {
                        printf("\nError creating new thread");
                }
        }

        printf("\nindex = %d", seq_res);                // the sequential sum here is 1000

        for (int i = 0; i < NUM_THREADS; i++) {
                int sum = 0;
                res = pthread_join(thread[i], (void **)&sum);
                if (res != 0) {
                        printf("\nError creating new thread");
                }
                printf("\nindex = %d", seq_res);        // Here it is becoming zero!!!

                par_res += sum;
        }

        printf("\nmultithreaded sum: %d single threaded sum: %d\n", par_res, seq_res);
}
4

1 回答 1

1

当你编译你的程序时,尽量消除警告,因为它们经常指出不可移植的行为或隐藏的错误。这里编译指出以下几点:

pte.c: In function 'segment_sum':
pte.c:11:21: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
   11 |         int index = (int)args;
      |                     ^
pte.c:18:16: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
   18 |         return (void *)sum;
      |                ^
pte.c: In function 'main':
pte.c:36:69: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
   36 |                 res = pthread_create(&thread[i], NULL, segment_sum, (void *)i);
      |                                                                     ^

传递给线程的参数是将指针转换为“int”。建议传递“int”的地址。因此,您可以定义每个线程的上下文:

struct thd_ctx {
  pthread_t thread;
  int index;
  int sum;
};

pthread_join() 传递了一个指针的地址,该指针将获取线程存储其结果的内存位置的地址。线程必须返回此内存位置的地址,而不是存储在其中的值。此外,线程不应返回未指定的自动变量的地址(即在其堆栈中)。结果必须是直接或通过pthread_exit()返回的全局变量(或从加入线程可见的“某物”)的地址。在程序的这个增强中,我们使用线程上下文中“sum”字段的地址:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>

#define SEGMENT_SIZE 10
#define NUM_THREADS 100
int *array = NULL;


struct thd_ctx {
  pthread_t thread;
  int index;
  int sum;
};


void *segment_sum(void *args)
{
  int i;
  struct thd_ctx *ctx = (struct thd_ctx *)args;

  ctx->sum = 0;

  for (i = ctx->index * SEGMENT_SIZE; i < (ctx->index + 1) * SEGMENT_SIZE; i++) {
    ctx->sum += array[i];
  }

  return (void *)&(ctx->sum);
}


int main(void)
{
  struct thd_ctx thd_ctx[NUM_THREADS];
  int res = 0;
  int seq_res = 0;
  int par_res = 0;
  int i;

  array = calloc(1, sizeof(int) * NUM_THREADS * SEGMENT_SIZE);
  if (!array) {
    fprintf(stderr, "calloc(): error %d\n", errno);
    return 1;
  }

  for (i = 0; i < NUM_THREADS * SEGMENT_SIZE; i++) {
    array[i] = 1;
    seq_res += 1;
  }

  for (i = 0; i < NUM_THREADS; i++) {
    thd_ctx[i].index = i;
    res = pthread_create(&(thd_ctx[i].thread), NULL, segment_sum, (void *)&(thd_ctx[i]));
    if (res != 0) {
      fprintf(stderr, "Error %d creating new thread#%d\n", res, i);
      free(array);
      return 1;
    }
  }

  printf("Index = %d\n", seq_res);   // the sequential sum here is 1000

  for (i = 0; i < NUM_THREADS; i++) {
    int *sum = 0;
    res = pthread_join(thd_ctx[i].thread, (void **)&(sum));
    if (res != 0) {
      printf("Error %d joining thread#%d", res, i);
      free(array);
      return 1;
    }

    par_res += *sum;

    printf("sum = %d\n", par_res);
  }

  printf("\nMultithreaded sum: %d single threaded sum: %d\n", par_res, seq_res);

  free(array);

  return 0;
}
于 2020-10-22T06:04:33.233 回答