[재질문 소스포함] 뮤텍스 extern으로 선언시... 에러가..
글쓴이: stypr / 작성시간: 수, 2005/01/26 - 4:42오후
main.c 파일 내용
#include <stdlib.h> #include <pthread.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <stdio.h> extern int abcd; void cdef(void *arg); void abcde(void *arg); extern pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER; int main() { pthread_t tid; pid_t pid; if ( pthread_create(&tid, NULL, (void*) &abcde, NULL )) { printf("error creating thread."); } if ( pthread_create(&tid, NULL, (void*) &cdef, NULL )) { printf("error creating thread."); } sleep(5); } void abcde(void *arg) { //int abcd; pthread_detach(pthread_self()); pthread_mutex_lock(&mymutex); abcd++; printf("=>%d\n", abcd); fflush(stdout); pthread_mutex_unlock(&mymutex); }
send_file.c 파일 내용
#include <pthread.h> #include <stdio.h> int abcd; pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER; void cdef(void *arg) { int abcd; pthread_detach(pthread_self()); pthread_mutex_lock(&mymutex); abcd++; printf("=>%d\n", abcd); fflush(stdout); pthread_mutex_unlock(&mymutex); }
컴파일
# gcc -o main main.c send_file.c -lpthread main.c:14: warning: `mymutex' initialized and declared `extern' send_file.c:16:2: warning: no newline at end of file /tmp/cc3C3NOu.o(.data+0x0): multiple definition of `mymutex' /tmp/ccKBnSkV.o(.data+0x0): first defined here collect2: ld returned 1 exit status
왜 이러는지 ㅠ.ㅠ 알려주세욥 ㅠ.ㅠ
흐흐 답변 부탁드립니다 ㅠ.ㅠ
Forums:
ㅎㅎㅎextern은 외부 변수로 선언하는 문장인데..거기에 값을
ㅎㅎㅎ
extern은 외부 변수로 선언하는 문장인데..
거기에 값을 대입해 주니 컴파일러가 기분 나빠지져..ㅋㅋㅋ
ㅉㅉㅉ
extern은 말 그대로 지금 여기 말고 다른 곳에 값 포함해서 선언해 놨어!
그런데
다른 곳에서 또 값을 선언하니깐
alofree님 말씀대로 기분이 나빠서..ㅋㅋㅋ
extern pthread_mutex_t mymutex=PTHREAD_M
extern pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER;
라는게 틀린건데요.
extern 으로 부른 변수는 선언할때 초기화를 하면 안됩니다.
extern pthread_mutex_t mymutex;
이렇게 하세요.
초기화는 mymutex 를 원래 생성하는 소스에서 담당하는거죠.
다시 초기화 하고 싶다면
main 함수에서 하세요.
부연하면 이건 소스 파일을 작성하는 요령에 관한 건데요.여러 소스에서
부연하면 이건 소스 파일을 작성하는 요령에 관한 건데요.
여러 소스에서 참조하는 이름은 헤더에 넣고 소스에서 #include 하세요.
send_file.h
send_file.c
main.c
댓글 달기