본문 바로가기

IT

print.c

// interthread cooperation을 보여줌.


// process를 fork 했을 때와는 달리, thread는 global variable을 이용해 협업할 수 있다.



#include <stdio.h>

#include <pthread.h>


#define NUM 15


int counter = 0;


main()

{

        pthread_t t1;

        void *print_count(void*);

        int i;

        pthread_create(&t1,NULL,print_count,NULL);

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

        {

                counter++;

                sleep(1);

        }

        pthread_join(t1,NULL);

}

void *print_count(void *m)

{

        int i;

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

        {

                printf("count = %d\n", counter);

                sleep(1);

        }

        return NULL;

}

'IT' 카테고리의 다른 글

two word 2  (0) 2014.12.09
two word count 1 .c  (0) 2014.12.09
multi.c  (0) 2014.12.09
genericClient  (0) 2014.11.23
genericServ.c  (0) 2014.11.23