[재질문 소스포함] 뮤텍스 extern으로 선언시... 에러가..

stypr의 이미지

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

왜 이러는지 ㅠ.ㅠ 알려주세욥 ㅠ.ㅠ

흐흐 답변 부탁드립니다 ㅠ.ㅠ

alofree의 이미지

ㅎㅎㅎ
extern은 외부 변수로 선언하는 문장인데..
거기에 값을 대입해 주니 컴파일러가 기분 나빠지져..ㅋㅋㅋ

xyzkkk의 이미지

extern은 말 그대로 지금 여기 말고 다른 곳에 값 포함해서 선언해 놨어!
그런데
다른 곳에서 또 값을 선언하니깐
alofree님 말씀대로 기분이 나빠서..ㅋㅋㅋ

ssehoony의 이미지

extern pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER;

라는게 틀린건데요.

extern 으로 부른 변수는 선언할때 초기화를 하면 안됩니다.

extern pthread_mutex_t mymutex;

이렇게 하세요.
초기화는 mymutex 를 원래 생성하는 소스에서 담당하는거죠.
다시 초기화 하고 싶다면
main 함수에서 하세요.

doldori의 이미지

부연하면 이건 소스 파일을 작성하는 요령에 관한 건데요.
여러 소스에서 참조하는 이름은 헤더에 넣고 소스에서 #include 하세요.

send_file.h

extern int abcd; 
extern pthread_mutex_t mymutex; 

void cdef(void *arg); 

send_file.c

#include "send_file.h"

int abcd; 
pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER; 

void cdef(void *arg) { /* ... */ }

main.c

#include "send_file.h"

int main(void) { /* ... */ }

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.