C++에서 다음 코드를 실행하고 있습니다.배열 오프셋 C++ pthreads 반환
예상대로 실행되지만 두 번째 for 루프에서는 첫 번째 결과가 NULL이지만 모든 결과는 정확합니다.
pthread_t pthread_t_array[thread_count];
int ireturn[thread_count];
float chunk_elements[thread_count];
for (int i = 0; i < thread_count; i++)
{
float vector_chunk[3] = {2.0,4.0,3.0};
pthread_t x;
pthread_t_array[i] = x;
ireturn[i] = pthread_create(&x,NULL,worker,(void *) vector_chunk);
}
for (int i = 0; i < thread_count; i++)
{
void * result;
ireturn[i] = pthread_join(pthread_t_array[i],&result);
// THE LINES BELOW: AT i = 0, result = NULL
// however, at every other value of i, 14 is returned as expected.
if (result != NULL){
chunk_elements[i] = *(float *) result;
}
free(result);
}
전체 코드는 here입니다.
왜 배열 오프셋이 있을지 확신하지 못합니다. 결과가 NULL 인 경우 테스트 할 라인에서 - i = 0, 결과는 NULL이지만, 다른 모든 i는 그렇지 않습니다.) 모든 것이 올바르게 진행되고있는 것처럼 보입니다.
수정 : 실제로는 오프셋이 아니라 첫 번째 값이 NULL이므로 값이 하나 더 적습니다.
유효한'pthread_t '가 아니기 때문에'pthread_t_array [i]'에 참여할 수 없습니다. (배열에있는 것이 아니라 (불필요한) 변수'x'에 저장했습니다.) – molbdnilo
@molbdnilo 내가 무슨 생각을했는지 모릅니다. – bordeo