Advertisement
jckuri

Start thread function with parameters and return value

Jun 25th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. // gcc -o thread_function thread_function.c
  2.  
  3. #include <stdio.h>
  4. #include <pthread.h>
  5.  
  6. void* thread_function(void *parameters) {
  7.  int number=(int)*((int*)parameters);
  8.  printf("number=%d\n",number);
  9.  char *a = malloc(10);
  10.  if(number==1) strcpy(a,"<Hello World>");
  11.  else strcpy(a,"<Good Night>");
  12.  pthread_exit((void*)a);
  13.  printf("After pthread_exit()\n");
  14. }
  15.  
  16. void* startThreadFunction(void*(*start_routine)(void*),void *parameters) {
  17.  pthread_t thread_id;
  18.  pthread_create (&thread_id,NULL,start_routine,parameters);
  19.  void *returnValue;
  20.  pthread_join(thread_id,&returnValue);  
  21.  return returnValue;
  22. }
  23.  
  24. int main(void) {
  25.  int number;
  26.  void *pointer;
  27.  number=2;
  28.  pointer=startThreadFunction(thread_function,&number);
  29.  printf("FINAL STRING: %s POINTER: %p\n",(char*)pointer,pointer);
  30.  number=1;
  31.  pointer=startThreadFunction(thread_function,&number);
  32.  printf("FINAL STRING: %s POINTER: %p\n",(char*)pointer,pointer);
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement