2013-11-25 7 views
1

그래서 코드를 실행할 때 pthread_join에서 세분화 오류가 발생합니다. 실행되지 않는 pthread_join 다음에 print 문이 있습니다. 왜 아무 생각 없어? 이 점을 이해하는 방법에 대한 힌트 나 아이디어를 알려주시겠습니까 ??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; 
} 

여기 매트릭스 CALC 기능입니다 :

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; 

} 
+0

당신은 –

+0

당신이 matrixCalc 기능을 붙여 넣을 수 ... pthread_create' 실패'여부를 확인해야합니까? – tyilmaz

+0

@BitFiddlingCodeMonkey, x = pthread_create를 설정하고 x! 0,하지만 = 0인지 확인했습니다. – angyxpoo

답변

6

pthread_create()을 호출 할 때 void *(*)(void *) 유형의 기능 주소를 전달해야합니다. 코드가 수행하는 기능은 함수 호출이므로 결과는 pthread_create()으로 전달됩니다.

변경이 줄을

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

가되기 위해

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

또는 사실상 동일 whic

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

.


이미 언급 한 것처럼 스레드 함수는 void *(*)(void *)으로 선언되어야합니다. 코드가 여러 스레드를 산란하려고하는 것 모두가 여러 가지의 pthread를 저장하는 perpare 방에 가입해야합니다으로

그래서이

void *matrixCalc(threadCount) 

void * matrixCalc(void *) 

가되기 위해 변경 -ids.다운


pthread_create(&tid[i], &attr, matrixCalc, NULL); 
스레드 번호 (카운터 i)를 건네 :

이 예 배열과 같이하여 수행 될 수있다 : 다음과 같이 스레드를 생성

pthread_t tid[numberOfThreads] = {0}; 

스레드에 또한 정의하여 방을주고

int thread_counts[numberOfThreads] = {0}; 
,210

할당 및 스레드 생성시 4 번째로 파라미터를 전달할 :

void * matrixCalc(void * pv) 
{ 
    int i, j, sum, tempNum, currentRow; 
    currentRow = *((int*) pv); 
    ... 
: 아래 스레드 함수
thread_counts[i] = i; 
pthread_create(&tid[i], &attr, matrixCalc, &thread_Counts[i]); 

다음과 같이

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

modifing에 의해 그것을 얻을


마지막으로 L 스레드는 루프에 의해 pthread_join()에 대한 단일 호출을 대체 :

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

와우! 이것은 효과가있다! 고맙습니다!!! (void *) 뒤에 매개 변수 이름을 넣어야했습니다. – angyxpoo

+0

매우 체계적인 답변, 명확하고 유용합니다! – Rob11311

1

의 INT는 pthread_create (가 pthread_t * 스레드, CONST pthread_attr_t의 *의 ATTR, void * (* start_routine) (void *), void * arg);

세 번째 매개 변수는 void ptr을 취하여 void ptr을 반환하는 시작 함수입니다.

네 번째 매개 변수는 전달할 데이터 (이 경우 threadcnt)를 가리키는 void ptr을 사용합니다.

+0

이 코드와 일치하도록 코드 줄을 변경하는 방법을 찾으려고합니다. 첫 번째 매개 변수에 대해서는 & tid를 사용하고 두 번째 매개 변수에는 & attr을 사용하고 세 번째 매개 변수에는 void (* matrixCalc) (void *)를 사용합니까? 또는 & matrixCalc? 내가 사용하려는 것에 오류가 있습니다. 그리고 threadCount를 가리키는 포인터를 만들려고 시도했지만 오류가 발생했습니다. 나는 단지 & threadCount – angyxpoo

+0

죄송합니다. 바쁜 하루입니다. 알의 대답을보십시오. 그는 꽤 많은 것을 다루었 다. FWIW에 대한 FAQ가 필요합니다. 'pthread' 질문의 적어도 3 분의 1은 정확히 이것에 관한 것 같습니다. – Duck

+0

예! 알크가 도와 줬어! 고마워요 – angyxpoo