플밍

윈도우즈에서 PThread 를 사용해 보자 본문

프로그래밍/C/C++

윈도우즈에서 PThread 를 사용해 보자

너구리안주 2013. 10. 26. 13:54

PThread는 (Posix Thread) 유닉스계열의 표준 스레드 라이브러리이다.

비록 유닉스계열의 라이브러리이지만 win32에서도 사용할 수 있다.





#ads_1

<< 다운로드 및 설치 방법 >>

1. http://www.sourceware.org/pthreads-win32/ 사이트에서 Download링크를 눌러 FTP 주소로 들어가자.

2. 현재 글쓰는 시점의 최신버전이  prebuilt-dll-1-11-0-release 이기에 이 디렉토리로 들어간다.

3. 해당 디렉토리 내의 include와 lib 디렉토리를 다운로드 받는다.

4. 다운받은 디렉토리내의 dll 파일들을 c:\windows\System32 디렉토리에 모두 복사한다.

5. 비주얼 스튜디오를 연다.

6. 프로젝트 -> 속성 -> VC++ 디렉터리를 연다.

7. 포함디렉터리에 include 디렉터리의 경로를 입력한다.

8. 라이브러리 디렉터리에 lib 디렉터리의 경로를 입력한다.

9. 프로젝트 -> 속성 -> 링커 -> 입력 -> 추가 종속성에 lib 파일들의 파일명을 모두 입력해 준다.

10. 프로젝트 -> 속성 -> C/C++ -> 일반 -> 추가 포함 디렉터리에 include과 lib의 경로를 모두 입력해 준다.



<< Pthread 한글 레퍼런스 사이트 >>

http://www.joinc.co.kr/modules/moniwiki/wiki.php/Site/Thread/Beginning/PthreadApiReference



<< 사용방법 >>

1. 기본적 사용방법

#include <stdio.h>
#include <pthread.h>
#include <Windows.h>


void* doFoo(void* data){

    int t_id = *((int*)data);

    for(int i=0; i<10; i++){
        printf("thread id(%d) : %d\n", t_id, i);
        Sleep(500);
    }

    return NULL;
}


int main(){

    pthread_t thread[3];

    int joinStatus;
    int f1 = 1;
    int f2 = 2;
    int f3 = 3;

    pthread_create(&thread[0], NULL, doFoo, (void*)&f1); //스레드를 생성하고 작동시킨다
    pthread_create(&thread[1], NULL, doFoo, (void*)&f2);
    pthread_create(&thread[2], NULL, doFoo, (void*)&f3);

    pthread_join(thread[0], (void**)&joinStatus); //스레드가 끝날때까지 기다린다
    pthread_join(thread[1], (void**)&joinStatus);
    pthread_join(thread[2], (void**)&joinStatus);

    printf("Main End!");

}


결과 : (아래와 같이 다른 스레드의 작업순서와 상관없이 지 할일 알아서 잘한다)

thread id(2) : 0
thread id(1) : 0
thread id(3) : 0
thread id(2) : 1
thread id(1) : 1
thread id(3) : 1
thread id(2) : 2
thread id(1) : 2
thread id(3) : 2
thread id(2) : 3
thread id(1) : 3
thread id(3) : 3
thread id(2) : 4
thread id(1) : 4
thread id(3) : 4


#ads_2


2. Mutex(임계영역의 상호배제 : 쉽게 말하면 어떤 부분을 처리하는 동안 lock을 걸어 동기화 시켜주는 것) 사용방법

#include <stdio.h>
#include <pthread.h>
#include <Windows.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int nCount = 0;

void* doFoo(void* data){

    int t_id = *((int*)data);

    pthread_mutex_lock(&mutex);  // 아래 작업들을 lock을 걸어 끝날때까지 다른 스레드가 침범하지 못하게 한다

    for(int i=0; i<10; i++){
        printf("thread id(%d) : %d\n", t_id, nCount++);
        Sleep(500);
    }

    pthread_mutex_unlock(&mutex);  //lock을 해제한다

    return NULL;
}


int main(){

    pthread_t thread[3];

    int joinStatus;
    int f1 = 1;
    int f2 = 2;
    int f3 = 3;

    pthread_mutex_init(&mutex, NULL);

    pthread_create(&thread[0], NULL, doFoo, (void*)&f1);
    pthread_create(&thread[1], NULL, doFoo, (void*)&f2);
    pthread_create(&thread[2], NULL, doFoo, (void*)&f3);

    pthread_join(thread[0], (void**)&joinStatus);
    pthread_join(thread[1], (void**)&joinStatus);
    pthread_join(thread[2], (void**)&joinStatus);

    pthread_mutex_destroy(&mutex);

    printf("Main End!");

}


결과 : ( 보는 바와 같이 mutex 설정된 곳의 작업이 끝날때까지 다른 쓰레드가 침범하지 못하고 작업이 끝날때까지 기다린다)

thread id(1) : 0
thread id(1) : 1
thread id(1) : 2
thread id(1) : 3
thread id(1) : 4
thread id(3) : 5
thread id(3) : 6
thread id(3) : 7
thread id(3) : 8
thread id(3) : 9
thread id(2) : 10
thread id(2) : 11
thread id(2) : 12
thread id(2) : 13
thread id(2) : 14

#ads_3



Comments