简体   繁体   English

C编程-pthread_create()循环索引作为参数

[英]C programming - pthread_create() loop index as argument

I have following c code that has some problems. 我有以下c代码有一些问题。

I want to realize a matrix multiplication (1dim arrays) with threads 我想用线程实现矩阵乘法(1dim数组)

pthread_t thread[ARRAY_SIZE];


void* mmult(void* r){
   int row = (intptr_t) r;         
   int index;                                
   int column, tmp, size = ARRAY_SIZE;
   for(column = 0; column < size; column++){ 
      tmp = 0;
      for(index = 0; index < size; index++){
         tmp += MA[row * size + index]  *  MBT[column * size + index];
      }
      MC[row * size + column] = tmp;
   }

   pthread_exit(NULL);      


}
int main(void) {
   int size = ARRAY_SIZE, row, column;
   struct timeval start, end;
   double exectime;

   init_matrix(MA); //function to create Matrix
   init_matrix(MB);



   for(row = 0; row < size; row++){ /* create Array-Threads */

      if(pthread_create(&thread[row], NULL, &mmult,&row) != 0){ //also doesnt work with (void*) &row
         perror("Fehler beim Erstellen eines Feld-Threads");
         exit(EXIT_FAILURE);
      }
    }

   int i;
   for(i =0; row < size; i++){
      status = pthread_join(thread[i], NULL);
   }


   return 0;
}

I have Matrix MA and Matrix MB and Matrix MBT where the values are switched to multiply it with MA . 我有Matrix MAMatrix MB以及Matrix MBT ,其中的值已切换为与MA相乘。

The size of my Matrix is defined by ARRAY_SIZE * ARRAY_SIZE 我的Matrix的大小由ARRAY_SIZE * ARRAY_SIZE定义

I am trying to create a thread for every row of the matrix and multiply row with column in my function mmult . 我正在尝试为矩阵的每一行创建一个线程,并在函数mmult行与列mmult

The error message I get is : Segmentation fault (core dumped) 我收到的错误消息是: Segmentation fault (core dumped)

Even though you haven't created a Minimal, Complete, and Verifiable Example to show us (which you really should have) it's easy to guess what the problem is. 即使您尚未创建最小,完整和可验证的示例来向我们展示(您确实应该有),也很容易猜测出问题所在。

Take a closer look at how you create your thread: 仔细研究如何创建线程:

pthread_create(&thread[row], NULL, &mmult,&row)

Here you pass a pointer to the variable row . 在这里,您将指针传递给变量row Now for how you use the argument in the thread function: 现在介绍如何在线程函数中使用参数:

int row = (intptr_t) r;

But r is a pointer to an int , it's not the value of the current "row" when creating the thread. 但是r是一个指向 int指针 ,它不是创建线程时当前“行”的值。

It would be more correct to do 这样做会更正确

int row = *(int *) r;

But this will lead to another problem: All thread function will be called with the very same pointer, all pointing to the very same place. 但这将导致另一个问题:所有线程函数都将使用完全相同的指针调用,所有指针均指向相同的位置。 And since you have no control over when threads start to run, you don't know if all threads will get different values for row or if there might be duplicates. 而且,由于您无法控制线程何时开始运行,因此您不知道所有线程的row值是否都不同,或者是否存在重复值。

To solve this you need to pass the value of the row variable when creating the thread. 为了解决这个问题,您需要在创建线程时传递row变量的 This is one of the few places where it's considered okay to cast a value to a pointer. 这是认为可以将值强制转换为指针的少数几个地方之一。 It actually needs two casts: 实际上,它需要两个强制转换:

pthread_create(&thread[row], NULL, &mmult,(void *) (intptr_t) row)

Now you need to do the opposite when getting the value inside the thread: 现在,当在线程内部获取值时,您需要做相反的事情:

int row = (int) (intptr_t) r;

Your function is already a void * , there's no need to pass it by reference. 您的函数已经是一个void * ,无需通过引用传递它。 You're passing a reference to the void * instead of passing void * itself. 您正在传递对void *的引用,而不是传递void *本身。 Instead try pthread_create(&thread[row], NULL, mmult, &row) 而是尝试pthread_create(&thread[row], NULL, mmult, &row)

This is the syntax of the function pthread_create . 这是函数pthread_create的语法。

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);

The first argument is of type pthread_t and represents the thread id although there might be some other errors. 第一个参数的类型为pthread_t ,尽管可能存在其他一些错误,但它表示线程ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM