2003. 6. 5. 16:05

posix thread

OldPapers/linux 2003. 6. 5. 16:05
쓰레드 프로그래밍에는 2가지 형태의 표준이 있는데, 하나가 이전에
소개했던 Solaris 쓰레드(Unix Internal 표준)이고, 다른 하나가
이번에 소개할 POSIX 쓰레드이다. POSIX는 IEEE 1003.4a로 표준이
이루어졌다. 따라서 POSIX 쓰레드를 사용하는 경우에 POSIX 쓰레드를
지원하는 어떠한 프로그래밍 환경에서도 동일한 API를 이용해서
프로그래밍할 수 있다.

현재 SunOS 5.5이상에서는 기본적으로 POSIX 쓰레드 라이브러리를
지원하고 있으며, 다른 버전에서는 POSIX 쓰레드 라이브러리를 설치하면
POSIX 쓰레드 프로그래밍을 할 수 있다.
SunOS 5.5에서 프로그래머는 원하는 경우에  Solaris 쓰레드와 POSIX
쓰레드를 동시에 이용해서 프로그래밍 할 수 있는 것이다.

다음은 간단한 POSIX 쓰레드를 이용해서 hello world를 출력하는
프로그램이다. POSIX 쓰레드 API는 모두 pthread_ 로 시작한다.

pthread_hello.c 파일
#define _POSIX_C_SOURCE 199506L #include <stdio.h> #include <pthread.h> void * sayHello(void * arg) { printf("Thread number: %d ::", pthread_self()); printf(" Hello World!!\n"); return (0); } main() { pthread_t tid; if(pthread_create(&tid, NULL, sayHello, NULL)) { printf("Thread Creation error!!\n"); exit(1); } printf("Thread number: %d\n", pthread_self()); pthread_exit( 0 ); }

프로그램에서 사용된 함수들을 간단히 살펴보자.


   1. pthread_create(): 쓰레드를 만들 때 사용된다. POSIX 쓰레드는 Solaris 쓰레드와는 다르게 쓰레드의 특성을 attribute 객체를 이용해서 기술한다. 따라서 쓰레드의 특성을 기술하고자 하는 경우에는 pthread_attr_t 타입의 변수를 선언 하고 사용해야 한다. 자세한 사항은 나중에 설명하도록 하겠다.

int pthread_create(pthread_t *new_thread_ID,
                   const pthread_attr_t *attr,
                   void * (*start_func)(void *), void *arg);

   2. pthread_exit(): 쓰레드를 종료할 때 사용된다.

void pthread_exit(void *status);

   3. pthread_self(): 쓰레드의 ID를 알고 싶을 때 사용된다.

pthread_t pthread_self(void)



생성된 쓰레드에 아규먼트를 전달하는 방법을 알아보자.
먼저 아규먼트 전체를 전달할 수 있는 자료 구조를 먼저 선언해야
한다. 예제 프로그램에서 아규먼트를 위해 Arglist라는 구조체를
선언했다.
두번째로 Arglist형의 변수에 원하는 값을 넣는다.
세번째로 Arglist형의 변수를 쓰레드를 만들 때 아규먼트로 전달
한다.

pthread_arg.c 파일
#define _POSIX_C_SOURCE 199506L #include <stdio.h> #include <pthread.h> struct Arglist { char * msg; int count; }; void * sayGreeting(void * ap) { struct Arglist *arg = (struct Arglist *) ap; int i; printf("Thread number: %d\n", pthread_self()); for(i = 0; i < arg->count; i++) printf("\t%s \n", arg->msg); return (0); } main() { pthread_t tid; struct Arglist arg; arg.msg = "How are you ?"; arg.count = 2; if(pthread_create(NULL, NULL, sayGreeting, (void *)&arg)) { printf("Thread Creation error!!\n"); exit(1); } printf("Thread number: %d\n", pthread_self()); pthread_exit( 0 ); }



쓰레드가 종료되면 종료 상태와 기타 정보가 보관된다. 쓰레드의 종료
상태는 때로는 중요한 정보가 되기 때문에 종료 상태를 알아볼 필요가
있다. 다음은 쓰레드의 종료 상태를 알아보는 예제이다.

pthread_join.c 파일
#define _POSIX_C_SOURCE 199506L #include <stdio.h> #include <pthread.h> void * sayHello(void * arg) { printf("Thread number: %d ::", pthread_self()); printf(" Hello World!!\n"); return (0); } main() { pthread_t tid; int status = -1; int * sp; if(pthread_create(&tid, NULL, sayHello, NULL)) { printf("Thread Creation error!!\n"); exit(1); } printf("Thread number: %d\n", pthread_self()); sp = &status; pthread_join(tid, (void **)&sp); printf("Thread number: %d has terminated.\n", tid); if(status) printf("And it's exit status is good.\n"); else printf("And it's exit status is bad.\n"); pthread_exit( 0 ); }


프로그램에서 사용된 함수들을 간단히 살펴보자.

   1. pthread_join(): 원하는 쓰레드의 종료 상태를 알아본다.

int pthread_join(pthread_t target_thread, void **status);