2010-05-22 3 views
2

pthread (저는 Eclipse IDE에서 링커와 컴파일러를 먼저 구성했습니다)를 사용하여 c로 일부 코드를 작성했습니다.pthread가있는 원격 함수

#include <pthread.h> 
#include "starter.h" 
#include "UI.h" 

Page* MM; 
Page* Disk; 
PCB* all_pcb_array; 

void* display_prompt(void *id){ 

    printf("Hello111\n"); 

    return NULL; 
} 


int main(int argc, char** argv) { 
    printf("Hello\n"); 
    pthread_t *thread = (pthread_t*) malloc (sizeof(pthread_t)); 
    pthread_create(thread, NULL, display_prompt, NULL); 
    printf("Hello\n"); 
    return 1; 
} 

잘 작동합니다. 그러나 display_prompt를 UI.h로 이동하면 "Hello111"출력이 인쇄되지 않습니다.

누구나 해결 방법을 알고 있습니까? Elad

답변

2

main이 반환되면 모든 스레드가 종료됩니다. 작성한 스레드가 그 순간에 아무 것도 인쇄하지 않으면 결코 실행되지 않습니다. 함수의 구현 위치가 아닌, 우연히 일어난다. 그런데

int main(int argc, char** argv) { 
    printf("Hello\n"); 
    pthread_t thread; 
    pthread_create(&thread, NULL, display_prompt, NULL); 
    printf("Hello\n"); 
    pthread_join(thread); 
    return 0; 
} 

: 스레드가 완료 될 때까지

main 대기를하려면 pthread_join를 사용

보내고 malloc에 대한 필요가 없습니다
  • ; 스택에 thread을 만들면됩니다.
  • 오류없이 끝난 경우 함수에서 0을 반환해야합니다.
+1

EXIT_SUCCESS 대신 0을 명시 적으로 반환하지 않고 기분이 좋습니다. – evilpie

+0

참. 그 가치가 절대로 바뀌지는 않지만 조금 더 명백합니다. – Thomas

+0

감사합니다. 내 문제가 달랐습니다. 모든 기능이 하나의 파일에있을 때 문제가 없었습니다. display_prompt()를 다른 파일로 옮기면 작동하지 않습니다. thread_join을 추가했지만 이제는 동일한 파일의 display_prompt()가 작동하지 않습니다. Eclipse에서 디버깅 할 수있는 특별한 방법이 있습니까? –