close

two thread 


#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>


void print_message_function ( void *ptr );


typedef struct str_thdata
{
        int thread_no;
        char message[100];
} thdata;


int main()
{
        pthread_t thread1, thread2;
        thdata data1, data2;


        data1.thread_no = 1;
        strcpy(data1.message, "Hello!");


        data2.thread_no = 2;
        strcpy(data2.message, "Hi!");


        pthread_create (&thread1, NULL, (void *) &print_message_function, (void *) &data1);
        pthread_create (&thread2, NULL, (void *) &print_message_function, (void *) &data2);


        pthread_join(thread1, NULL);
        pthread_join(thread2, NULL);


        exit(0);
}


void print_message_function ( void *ptr )
{
        thdata *data;
        data = (thdata *) ptr;


        printf("Thread %d says %s \n", data->thread_no, data->message);


        pthread_exit(0);
}

 

makefile


thread:thread.o
        cc -o thread thread.o -lpthread


thread.o:thread.c
        cc -c thread.c


clean:
        rm -rf thread *.o

arrow
arrow
    全站熱搜

    = = 發表在 痞客邦 留言(0) 人氣()