pthread_create() 는 각 thread의 시작 함수에 하나의 argument를 넘길 수 있게 해 준다. 그런데 만일 넘기고자 하는 argument가 여러개라면, 이때는 모든 argument들을 포함하는 structure의 형태로 만들어서 포인터로 넘겨주면 된다.

또한 argument들은 참조 호출 형태로 넘겨져야 하며 (void* ) 로 cast되어야 한다.

Example 1)
이 예제는 간단한 integer 형의 변수값을 argument로 넘긴다.

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define NUM_THREADS 4

const char *messages[NUM_THREADS] = {"English: Hello World!", "French: Bonjour, le monde!",
 "Spanish: Hola al mundo", "German: Guten Tag, Welt!"};

void *PrintHello(void* threadID)
{
  int *idPtr, taskID;
  sleep(1);
  idPtr = (int* ) threadID;
  taskID = *idPtr;
  printf("Thread %d: %s\n", taskID, messages[taskID]);
  pthread_exit(NULL);
}

int main(int argc, char* argv[])
{
  pthread_t threads[NUM_THREADS];
  int taskIDs[NUM_THREADS];
  int rc, t;

  for (t = 0; t < NUM_THREADS; t++){
    taskIDs[t] = t;
    printf("Creating thread %d\n", t);
    rc = pthread_create(&threads[t], NULL, PrintHello, (void* )&taskIDs[t]);
    if (rc){
      printf("ERROR; return code from pthread_create() is %d\n", rc);
      exit(-1);
    }
  }
  pthread_exit(NULL);
}

Example 2)
다음은 여러개의 argument들을 structure로 어떻게 넘기는지 볼 수 있는 좋은 예제이다.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define NUM_THREADS 4

const char *messages[NUM_THREADS] = {"English: Hello World!", "French: Bonjour, le monde!",
 "Spanish: Hola al mundo", "German: Guten Tag, Welt!"};


struct threadData
{
  int threadID;
  int sum;
  char* message;
};

struct threadData threadDataArray[NUM_THREADS];

void *PrintHello(void* arg)
{
  int taskID, sum;
  char* helloMsg;
  struct threadData *myData;

  sleep(1);
  myData = (struct threadData* ) arg;
  taskID = myData->threadID;
  sum = myData->sum;
  helloMsg = myData->message;

  printf("Thread %d: %s, Sum = %d\n", taskID, helloMsg, sum);
  pthread_exit(NULL);
}

int main(int argc, char* argv[])
{
  pthread_t threads[NUM_THREADS];
  int *taskIDs[NUM_THREADS];
  int rc, t, sum = 0;

  for (t = 0; t < NUM_THREADS; t++){
    sum += t;
    threadDataArray[t].threadID = t;
    threadDataArray[t].sum = sum;
    threadDataArray[t].message = (char* )messages[t];

    printf("Creating threads %d\n", t);

    rc = pthread_create(&threads[t], NULL, PrintHello, (void* )&threadDataArray[t]);
    if (rc){
      printf("ERROR; return code from pthread_create() is %d\n", rc);
      exit(-1);
    }
  }
  pthread_exit(NULL);
}

Example 3)
다음은 argument passing의 잘못 된 예이다. 코드를 보면 알겠지만 thread가 생성 될 때마다 argument의 변수 값이 계속 변하기 때문이다.

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define NUM_THREADS 4

void *PrintHello(void* threadID)
{
  int taskID;
  sleep(1);
  taskID = *threadID;
  printf("Hello from thread %d\n", taskID);
  pthread_exit(NULL);
}

int main(int argc, char* argv[])
{
  pthread_t threads[NUM_THREADS];
  int rc, t;

  for (t = 0; t < NUM_THREADS; t++){
    printf("Creating threads %d\n", t);

    rc = pthread_create(&threads[t], NULL, PrintHello, (void* )&t);
    if (rc){
      printf("ERROR; return code from pthread_create() is %d\n", rc);
      exit(-1);
    }
  }
  pthread_exit(NULL);
}




[출처 : http://exahz.tistory.com/13]

+ Recent posts