IT

multi.c

kio467 2014. 12. 9. 18:00

// thread 를 이용하는 첫번째 코드.


// 컴파일할때 -lpthread 붙여줘야됨


// gcc multi.c -o multi -lpthread


#include <stdio.h>

#include <pthread.h>


#define NUM 15


void *print_msg(void *);


main()

{

        pthread_t t1, t2;                                                                 // thread 이름

        pthread_create(&t1,NULL, print_msg, (void*)"hello");                 // argu 를 구조체로 하면 여러개의 parameter를 넘길 수 있음.

        pthread_create(&t2,NULL,print_msg, (void*)"world\n");                 // (그래도 void로 타입 캐스팅은 해야됨)

        pthread_join(t1,NULL);                                                         // 스레드 끝날때가지 기다림

        pthread_join(t2,NULL);

}


void *print_msg(void *m)

{

        char *cp = (char *)m;

        int i;

        for (i=0;i<NUM;i++)

        {

                printf("%s",m);             // 어차피 저 cp는 안쓰는 데 왜 저렇게 했는지 모르겠네 걍 (char*)m하면 안되나

                fflush(stdout);

                sleep(1);

        }

        return NULL;

}