Giriş
İmzası şöyle
1. parametre thread nesnesini saklamak içindir. Yani "out" parametresidir.
2. parametre thread'e geçilmek istenen pthread_attr nesnesini belirtir. Genelde NULL kullanılır. Açıklaması şöyle
4. parametre thread'e ilave veri geçmek için kullanılır.
Klasik kullanımı şöyledir.
pthread_attr yapısı yazısına taşıdım.
3. thread'in ana metodu
thread'in ana metodunu belirtiriz.İmzası şöyledir. void* döner ve parametre olarak void* alır.
İmzası şöyle
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                      void *(*start_routine) (void *), void *arg);1. parametre thread nesnesini saklamak içindir. Yani "out" parametresidir.
2. parametre thread'e geçilmek istenen pthread_attr nesnesini belirtir. Genelde NULL kullanılır. Açıklaması şöyle
The attr argument points to a pthread_attr_t structure whose contents
are used at thread creation time to determine attributes for the new
thread; this structure is initialized using pthread_attr_init(3) and
related functions.  If attr is NULL, then the thread is created with
default attributes.4. parametre thread'e ilave veri geçmek için kullanılır.
Klasik kullanımı şöyledir.
pthread_create (&t, NULL, threadMain, NULL);void *testThread (void *pParm){...}pthread_create(&t, NULL, (void *)threadMain, NULL);pthread_create (&t, NULL, threadMain, NULL);pthread_create (&t, NULL, [](void* ptr){
  ...; 
  return (void*)nullptr;
}, NULL);pthread_attr yapısı yazısına taşıdım.
3. thread'in ana metodu
thread'in ana metodunu belirtiriz.İmzası şöyledir. void* döner ve parametre olarak void* alır.
void *myThread (void *arg) {
  ...
  return nullptr;
}struct threadresult {
  int sum;
  int product;
};
void* myThread (void* arg) {
  ...
  struct threadresult* result = malloc(sizeof(*result));
  result->sum = ...;
  result->product = ...;
  return result;
}
pthread_create() metodunun 4. alanı, başlatılan thread'e geçilmek istenen parametre. Eğer birden fazla parametre geçilmesi gerekiyorsa bir struct şeklinde geçilebilir. Şöyle bir struct'ımı olsun
struct dimension {
  unsigned int width;
  unsigned int height;
};// Pass a struct in pthread_create (NOT on the stack)
struct dimension *dim = new dimension();
dim->width = 1;
dim->height = 2;
pthread_create(&ph, &attr, myThread, dim);void *myThread(void* dimension) {
  struct dimension* dim = (struct dimension*) dimension;
  //Do work
    
  return 0;
}void* myThread (void* dimesion) {
  // Get thread args in usable type.
  struct dimension* dim = (struct dimension*) dimension;  ...
  delete dim;  return 0;
} 
 
Hiç yorum yok:
Yorum Gönder