IT

print.c

kio467 2014. 12. 9. 18:16

// 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;

}