Posix Thread 예제 소스

PThread 간단하게 쓰레드 생성하고 간단한 뮤텍스까지 들어있는 소스 입니다.
#include 
#include 
#include 
#include 
#include 

using namespace std;
pthread_mutex_t mutex;

struct arg
{
    int start;
    int end;
    int time;
    string name;
};


void *cnt(void* a)
{
    struct arg* temp=(struct arg*)a;

    for(int i= temp->start; i < temp->end;i++)
    {
        pthread_mutex_lock(&mutex);
        cout << temp->name <<" : " << i << endl;
        pthread_mutex_unlock(&mutex);
        sleep(temp->time);
    }
    pthread_exit(NULL);
}
int main()
{
    pthread_t td1,td2,td3;
    struct arg ar1,ar2,ar3;
    void* state;
    ar1.start=0;
    ar1.end=20;
    ar1.name=string("thread1");
    ar1.time=1;

    ar2.start=-10;
    ar2.end=0;
    ar2.name=string("thread2");
    ar2.time=2;

    ar3.start=100;
    ar3.end=110;
    ar3.name=string("thread3");
    ar3.time=3;
    pthread_mutex_init(&mutex,NULL);
    pthread_create(&td1,NULL,cnt,(void*)&ar1);
    pthread_create(&td2,NULL,cnt,(void*)&ar2);
    pthread_create(&td3,NULL,cnt,(void*)&ar3);

    pthread_join(td1,&state);
    pthread_join(td2,&state);
    pthread_join(td3,&state);

    pthread_mutex_destroy(&mutex);

    return 0;
}

'Computer 기타' 카테고리의 다른 글

C# UnitTest by csUnit  (0) 2010.07.02
Pthread with Semaphore <posix>  (0) 2010.03.16
자바 Tar 묶음 파일 만들기  (0) 2010.02.27
자바 - 정규표현식 예제입니다.<혼자보기용>  (0) 2010.02.27