2011-08-10 2 views
0

스레드를 생성하고 구조체의 일부 변수를 선언하는 위치에서 세그먼트 오류가 계속 발생합니다 ...
이유를 아는 사람이 있습니까?c, pthread_create가 세그멘테이션 오류를 발생합니까?

pthread_create(&threads[thread_id]->thread, NULL, worker_threadloop, (void *)(long)i); 

: 가정

threads[]->thread 

dispatch_queue_t *dispatch_queue_create(queue_type_t queueType){ 

int cores = num_cores(); 
printf("message-queuecreate1"); 

dispatch_queue_t *dispatch_queue = (dispatch_queue_t *) malloc(sizeof(dispatch_queue_t)); 
dispatch_queue->HEAD = NULL; 
dispatch_queue->END = NULL; 

//create a thread array for dispatcher and worker threads 
dispatch_queue_thread_t *threads[cores+1]; 
threads[cores+1]= (dispatch_queue_thread_t *)malloc(sizeof(dispatch_queue_thread_t)); 

//create semaphores 
sem_t queue_task_semaphore; 
sem_t queue_thread_semaphore; 
sem_t queue_wait_semaphore; 

sem_init(&queue_task_semaphore, 0, 1); 
sem_init(&queue_thread_semaphore,0,1); 
sem_init(&queue_wait_semaphore,0,1); 

dispatch_queue->queue_task_semaphore = queue_task_semaphore; 
dispatch_queue->queue_thread_semaphore = queue_thread_semaphore; 
dispatch_queue->queue_wait_semaphore = queue_wait_semaphore; 




//create dispatcher thread  
//segmentation fault #1//////////////// 
threads[0]->current_task=NULL; 
threads[0]->queue=dispatch_queue; 

pthread_create(threads[0]->thread, NULL, dispatcher_threadloop, threads[0]); 
////////////////////////////////////// 

if (queueType == CONCURRENT){ 
    int i = 0; 
    int thread_id=0; 
    //create worker thread array if the type of the queue is concurrent 

    while(i<cores){ 

     //create worker thread 

     thread_id = i+1; 

     //segmentation fault #2////////// 
     threads[i+1]->queue=dispatch_queue; 
     threads[thread_id]->thread=NULL; 
     threads[thread_id]->current_task=NULL; 

     pthread_create(threads[thread_id]->thread, NULL, worker_threadloop, (void *)(long)i); 
     //////////////////////////////// 

     printf("thread %d is created\n",i); 
     i++; 
    } 

} else {//do smth} 
    //segmentation fault# 3//////////////// 
    threads[1]->thread=worker_thread; 
    threads[1]->queue=dispatch_queue; 
    threads[1]->current_task=NULL; 
    ////////////////////////////////////// 
} 



return dispatch_queue; 

} 

답변

2

귀하의 코드는 문제 투성이입니다 :

액세스 스레드 [코어 + 1] 유효하지 않습니다. 또한 threads[0] ... threads[core]에 대한 메모리를 할당하지 않았습니다.

dispatch_queue_thread_t *threads[cores+1]; 
threads[cores+1]= ....; 

그래서이 실패합니다 :

threads[0]->current_task=NULL; /* See above. */ 

threads[i+1]->queue=dispatch_queue; /* Again, no memory allocated. */ 


가 다른 문제가 될 수 있지만, 나는 cores+1 물건을 파괴력과로 교체하여 시작할 것 : 나는 것을 시도

for (i = 0; i < cores; i++) { 
    threads[i] = malloc(sizeof(*threads[i])); 
} 
+0

네, 이것은 당신의 버그입니다. –

+0

와우 고맙습니다 대부분의 결함을 없애 버리지 만 스레드를 만들면 세그먼트 화 오류가 발생합니다 .. 예 :'pthread_create (& threads [0] -> thread, NULL, dispatcher_threadloop, threads [0]); ' – Leanne

+0

@Leanne 귀하의 질문을 편집하고'dispatch_queue_thread_t'의 정의를 추가하십시오. – cnicutar

1

당신은 참조를 제공 할 필요가 pthread_t (아닌가 pthread_t의 *)

입니다.

+0

yeayea , 그리고 그것은'dispatchQueue.c : 72 : warning : 'pthread_create'의 인수 1을 호환되지 않는 포인터 타입으로부터 전달하는 에러를냅니다. – Leanne

+0

오, 잠깐. 하지만 여전히 그 세분화 오류가 있습니다 .. – Leanne

관련 문제