我目前正在处理一个作业任务,该任务使用多个线程执行矩阵乘法,但是我不断收到错误消息,指出所有pthread均创建失败。我将一些代码片段放在下面,这些代码片段与线程的分配空间,线程的工作块以及pthread_create和pthread_join一起使用,然后threadMMulti是每个线程使用的函数。我的错误来自这段代码
if (errorCode = pthread_create(&threadHandles[i], NULL, threadMAdd,
&blocksOfWork[i]) != 0) {
printf("pthread %d failed to be created with error code %d\n", i, errorCode);
因此,根据我的理解,如果创建了线程,则pthread_create将返回0,因此很明显pthread_create失败并返回一个非0的数字,因此显示了消息,我查看了pthread_create的参数,我可以似乎找不到我要去哪里。如果有人在正确的方向上有任何技巧或指向,我将不胜感激。
代码段:
typedef struct {
int threadId;
int start_row;
int end_row;
int start_col;
int end_col;
} BLOCK;
// Generate arrays for threads handles
threadHandles = (pthread_t *) malloc(numberOfThreads*sizeof(pthread_t));
blocksOfWork = (BLOCK *) malloc(numberOfThreads*sizeof(BLOCK));
// allocate block of work for each thread
for(i=0; i < numberOfThreads; i++){
blocksOfWork[i].threadId = i;
blocksOfWork[i].start_row = i * rows/numberOfThreads;
if (i == numberOfThreads -1){
blocksOfWork[i].end_row = rows - 1;
}
else{
blocksOfWork[i].end_row = (i+1)*rows/numberOfThreads -1;
}
blocksOfWork[i].start_col = 0;
blocksOfWork[i].end_col = columns -1;
}
for (i=0; i < numberOfThreads; i++) {
if (errorCode = (pthread_create(&threadHandles[i], NULL, threadMMult,
&blocksOfWork[i])) != 0) {
printf("pthread %d failed to be created with error code %d\n", i, errorCode);
} // end if
} // end for
for (i=0; i < numberOfThreads; i++) {
if (errorCode = pthread_join(threadHandles[i], (void **) NULL) != 0) {
printf("pthread %d failed to be joined with error code %d\n", i, errorCode);
} // end if
} // end for
void * threadMMult(void * arg){
BLOCK * block = (BLOCK *) arg;
int threadId = block->threadId;
int startRow = block->start_row;
int endRow = block->end_row;
int startCol = block->start_col;
int endCol = block->end_col;
int i, j, k;
for (int i =startRow; i<=endRow;i++){
for (int j =startCol; i<=endCol; j++){
C[i][j] = 0;
for(int k =0; k<=endCol; k++){
C[i][j] += A[i][k]*B[k][j];
}
}
}
}
答案 0 :(得分:0)
这对我有效(在Linux下)。
可能是您的BLOCK
处理中的问题了吗?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
// Compile with: gcc -g -Wall threadmult.cpp -o threadmult -lpthread -lstdc++
typedef struct
{
int threadId;
int start_row;
int end_row;
int start_col;
int end_col;
} BLOCK;
void * threadMMult(void * arg)
{
printf("threadMMult()- Thread=%lu with %p BEGINS\n", pthread_self(), arg);
// TODO - some stuff
printf("threadMMult()- Thread=%lu with %p ENDS\n", pthread_self(), arg);
return NULL;
}
int main(void)
{
int i;
int errorCode;
int numberOfThreads = 7;
// Generate arrays for threads handles
pthread_t *threadHandles = (pthread_t *) malloc(numberOfThreads*sizeof(pthread_t));
BLOCK *blocksOfWork = (BLOCK *) malloc(numberOfThreads*sizeof(BLOCK));
// allocate block of work for each thread
for (i=0; i < numberOfThreads; i++)
{
for (i=0; i < numberOfThreads; i++)
{
errorCode = (pthread_create(&threadHandles[i], NULL, threadMMult, &blocksOfWork[i]));
if (errorCode != 0)
{
printf("pthread %d failed to be created with error code %d", i, errorCode);
switch (errorCode)
{
// Note to students - don't format your switch() like this
// This is OK here, because this is a quick-and-dirty debug program
case EAGAIN: printf(", which is EAGAIN\n"); break;
case EINVAL: printf(", which is EINVAL\n"); break;
case EPERM: printf(", which is EPERM\n"); break;
default: printf(", which is unknown\n");
}
}
}
}
return 0;
}