【问题标题】:Segmentation Fault at pthread_joinpthread_join 的分段错误
【发布时间】:2013-11-25 18:37:51
【问题描述】:

所以当我运行我的代码时,我在 pthread_join 处遇到了分段错误。在我的 pthread_join 之后有一个打印语句没有运行。有谁知道为什么?你能给我一些关于如何解决这个问题的提示或想法吗?

输出打印出我的矩阵的所有行号,直到结束,然后它离开 matrixCalc 函数并打印“创建线程后”。当我为 1 个线程输入参数时,就会发生这种情况。

我在这里包含了我的一小部分代码:

int main(int argc, char*argv[]) 
{
  //takes in number of threads as 1st arg
  pthread_attr_init(&attr);
  //initialize matrix here

  //passes num of threads through matrixcalc
  for(i = 0; i < numberOfThreads; i++)
    {
      threadCount++;
      pthread_create(&tid, &attr, matrixCalc(threadCount), NULL);  
    }
  printf("after threads are created\n");
  pthread_join(tid, NULL);  
  printf("after join\n");
  exit(0);
  return 0;
}

这里是矩阵计算函数:

    void *matrixCalc(threadCount) 
{
  int i, j, sum, tempNum, currentRow;
  currentRow = threadCount;
  sum=0;

  while(currentRow < 1200)
    {
      //cycles through the column j for matrix B
      for(j=0; j<500; j++)
        {
          //cycles through the diff i values for the set row in matrix A and column in matrix B
          for(i=0; i<1000; i++)
            {
              //Matrix A set i value is at threadcount-1
              //Matrix B i value = j
              //Matrix B j value = i
              //Multiply together and add to sum
              tempNum = (matrixA[currentRow-1][i])*(matrixB[i][j]);
              sum = sum+tempNum;
            }
          //Set Matrix C at i value = currentRow and jvalue = i to sum
          matrixC[currentRow-1][j] = sum;
          //printf("%d\n", matrixC[currentRow-1][i]);
        }
        //increase threadcount by number of threads 
        //until you hit max/past max val
        currentRow = currentRow + nThreads;
        //printf("%d\n", currentRow);
    }
    return NULL;

}

【问题讨论】:

  • 你应该检查pthread_create是否失败...
  • 可以粘贴matrixCalc函数吗?
  • @BitFiddlingCodeMonkey,我设置 x = pthread_create 并检查 x 是否为 !0,但它是 =0。
  • @angyxpoo pthread_create() == 0 对于每个线程?
  • @tyilmaz 我发布了矩阵计算

标签: c matrix pthreads posix pthread-join


【解决方案1】:

调用pthread_create() 时,您需要传递void *(*)(void *) 类型的函数的地址。代码所做的是在那里调用一个函数,以便将其结果传递给pthread_create()

改变这一行

pthread_create(&tid, &attr, matrixCalc(threadCount), NULL);  

成为

pthread_create(&tid, &attr, matrixCalc, NULL);  

pthread_create(&tid, &attr, &matrixCalc, NULL);  

其实是一样的。


如上所述,线程函数需要声明为void *(*)(void *)

所以改变这个

 void *matrixCalc(threadCount) 

会变成这样

 void * matrixCalc(void *) 

由于代码似乎试图生成多个线程,并且所有线程都应该连接起来,以便腾出空间来存储多个 pthread-id。

这可以例如使用这样的数组来完成:

pthread_t tid[numberOfThreads] = {0};

然后像这样创建线程:

pthread_create(&tid[i], &attr, matrixCalc, NULL);

要将线程号(计数器i)传递给线程,也可以通过定义给它空间

int thread_counts[numberOfThreads] = {0};

分配它并将其作为线程创建时的第 4th 参数传递:

 thread_counts[i] = i;
 pthread_create(&tid[i], &attr, matrixCalc, &thread_Counts[i]);

在线程函数中下来,然后通过修改得到它

void *matrixCalc(threadCount) 
{
  int i, j, sum, tempNum, currentRow;
  currentRow = threadCount;
  ...

像这样:

void * matrixCalc(void * pv) 
{
  int i, j, sum, tempNum, currentRow;
  currentRow = *((int*) pv);
  ...

最后加入所有线程,用循环替换对pthread_join()的单个调用:

for (i = 0; i < numberOfThreads; ++i)
{
  pthread_join(tid[i], NULL);  
}

【讨论】:

    【解决方案2】:

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

    第三个参数是一个 start 函数,接受一个 void ptr 并返回一个 void ptr。

    第四个参数接受一个指向你要传递的数据的 void ptr,在本例中为 threadcnt。

    【讨论】:

    • 我试图弄清楚如何更改我的代码行以匹配此。对于第一个参数,我会使用 &tid,对于第二个参数,我会使用 &attr,对于第三个参数,我会使用 void (*matrixCalc)(void *) 吗?还是 &matrixCalc?我正在尝试使用的内容出现错误。我试图创建一个指向我的 threadCount 的指针,但出现错误。我会做 &threadCount
    • 抱歉,今天很忙。查看alk的答案。他似乎已经涵盖了很多。 FWIW 我们需要对此有一个常见问题解答。至少有三分之一的pthread 问题似乎与此有关。
    猜你喜欢
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 2019-07-21
    • 2017-04-18
    • 2016-08-23
    • 1970-01-01
    相关资源
    最近更新 更多