Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 18th, 2012  |  syntax: None  |  size: 1.50 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <unistd.h>    
  2. #include <sys/types.h>  
  3. #include <errno.h>      /* Errors */
  4. #include <stdio.h>      /* Input/Output */
  5. #include <stdlib.h>    
  6. #include <pthread.h>    /* POSIX Threads */
  7. #include <string.h>    
  8.  
  9. /* thread için prototipler */
  10. void print_message_function ( void *ptr );
  11.  
  12. /* thread verilerini daha derli toplu tutabilmek için struct yapısını kullacağız */
  13. typedef struct str_thdata
  14. {
  15.     int thread_no;
  16.     char message[100];
  17. } thdata;
  18.  
  19. int main()
  20. {
  21.     pthread_t thread1, thread2;  /* thread değişkenleri */
  22.     thdata data1, data2;         /* struct türüne geçirilmiş threadler */
  23.    
  24.     /* thread 1 için veri ilklendiriliyor*/
  25.     data1.thread_no = 1;
  26.     strcpy(data1.message, "Hello!");
  27.  
  28.     /* thread 2 için veri ilklendiriliyor*/
  29.     data2.thread_no = 2;
  30.     strcpy(data2.message, "Hi!");
  31.    
  32.     /* thread 1 ve 2 yaratılıyor */    
  33.     pthread_create (&thread1, NULL, (void *) &print_message_function, (void *) &data1);
  34.     pthread_create (&thread2, NULL, (void *) &print_message_function, (void *) &data2);
  35.  
  36.     pthread_join(thread1, NULL);
  37.     pthread_join(thread2, NULL);
  38.                
  39.     exit(0);
  40. }
  41. void print_message_function ( void *ptr )
  42. {
  43.     thdata *data;            
  44.     data = (thdata *) ptr;  /* thdata tipinde bir pointer atanıyor*/
  45.     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 */
  46.    
  47.     pthread_exit(0);
  48. } /* print_message_function ( void *ptr ) */