
Untitled
By: a guest on
Aug 18th, 2012 | syntax:
None | size: 1.50 KB | hits: 10 | expires: Never
#include <unistd.h>
#include <sys/types.h>
#include <errno.h> /* Errors */
#include <stdio.h> /* Input/Output */
#include <stdlib.h>
#include <pthread.h> /* POSIX Threads */
#include <string.h>
/* thread için prototipler */
void print_message_function ( void *ptr );
/* thread verilerini daha derli toplu tutabilmek için struct yapısını kullacağız */
typedef struct str_thdata
{
int thread_no;
char message[100];
} thdata;
int main()
{
pthread_t thread1, thread2; /* thread değişkenleri */
thdata data1, data2; /* struct türüne geçirilmiş threadler */
/* thread 1 için veri ilklendiriliyor*/
data1.thread_no = 1;
strcpy(data1.message, "Hello!");
/* thread 2 için veri ilklendiriliyor*/
data2.thread_no = 2;
strcpy(data2.message, "Hi!");
/* thread 1 ve 2 yaratılıyor */
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; /* thdata tipinde bir pointer atanıyor*/
printf("Thread %d says %s \n", data->thread_no, data->message);/* struct veri türü ve pointer kullanılarak bulunan sonuçlar ekrana yansıtılıyor */
pthread_exit(0);
} /* print_message_function ( void *ptr ) */