ang*_*poo 2 c posix pthreads matrix pthread-join
因此,当我运行我的代码时,我在pthread_join上遇到了分段错误.在我的pthread_join之后有一个没有运行的print语句.有谁知道为什么?你能给我一些关于如何解决这个问题的提示或想法吗?
输出打印出矩阵的所有行号,直到结束,然后它离开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;
}
Run Code Online (Sandbox Code Playgroud)
这是矩阵计算功能:
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;
}
Run Code Online (Sandbox Code Playgroud)
在调用时,pthread_create()您需要传递类型函数的地址void *(*)(void *).代码所做的是在那里调用一个函数,以便将结果传递给它pthread_create().
改变这一行
pthread_create(&tid, &attr, matrixCalc(threadCount), NULL);
Run Code Online (Sandbox Code Playgroud)
成为
pthread_create(&tid, &attr, matrixCalc, NULL);
Run Code Online (Sandbox Code Playgroud)
要么
pthread_create(&tid, &attr, &matrixCalc, NULL);
Run Code Online (Sandbox Code Playgroud)
实际上是一样的.
如上所述,线程函数需要声明为void *(*)(void *).
所以改变这个
void *matrixCalc(threadCount)
Run Code Online (Sandbox Code Playgroud)
将成为这个
void * matrixCalc(void *)
Run Code Online (Sandbox Code Playgroud)
由于代码似乎试图产生多个线程,所有应该连接perpare空间来存储几个pthread-id.
例如,可以使用如下数组来完成:
pthread_t tid[numberOfThreads] = {0};
Run Code Online (Sandbox Code Playgroud)
然后像这样创建线程:
pthread_create(&tid[i], &attr, matrixCalc, NULL);
Run Code Online (Sandbox Code Playgroud)
将线程号(计数器i)传递给线程也通过定义给它空间
int thread_counts[numberOfThreads] = {0};
Run Code Online (Sandbox Code Playgroud)
分配它并将其作为线程创建的第 4 个参数传递:
thread_counts[i] = i;
pthread_create(&tid[i], &attr, matrixCalc, &thread_Counts[i]);
Run Code Online (Sandbox Code Playgroud)
在线程函数中,然后通过修改获取它
void *matrixCalc(threadCount)
{
int i, j, sum, tempNum, currentRow;
currentRow = threadCount;
...
Run Code Online (Sandbox Code Playgroud)
像这样:
void * matrixCalc(void * pv)
{
int i, j, sum, tempNum, currentRow;
currentRow = *((int*) pv);
...
Run Code Online (Sandbox Code Playgroud)
最后加入所有线程pthread_join(),用循环替换单个调用:
for (i = 0; i < numberOfThreads; ++i)
{
pthread_join(tid[i], NULL);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15554 次 |
| 最近记录: |