KrunalParmar
KrunalParmar

Reputation: 1114

Passing data to Pthread using structure in C

I want to run Pthreads in c. i am comfortable with sending integer values to Pthread in c. But when I am passing the data structure to the thread, it is printing garbage values.

My code is as follows:

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


#define NUMBER_OF_THREADS   3

typedef struct MyStructure
{
      int id;
      char *myName;
}myst;

void *PrintHello(void *threadArgs)
{
      myst *myPersonalSt;

      myPersonalSt = (myst *) threadArgs;

      printf("Thread %d and Name : %s \n", myPersonalSt->id ,   
      myPersonalSt->myName);
      pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
      pthread_t threads[NUMBER_OF_THREADS];
      long taskids[NUMBER_OF_THREADS];
      int rc, t;
      char myNameArray[NUMBER_OF_THREADS][100] = {"kp" , "kkp" , "ap"};

      myst **mystArray;
      mystArray = (myst **)malloc(NUMBER_OF_THREADS*sizeof(myst));

      for(t=0;t<NUMBER_OF_THREADS;t++)
      {
                mystArray[t] = (myst *)calloc(1,sizeof(myst));
                mystArray[t]->id = t+1;
                mystArray[t]->myName = myNameArray[t];
      }

      for(t=0;t<NUMBER_OF_THREADS;t++) {

                printf("Creating thread %d\n", t);
                rc = pthread_create(&threads[t], NULL, PrintHello, 
                           (void  *) mystArray);

                if (rc) {
                          printf("ERROR; return code from 
                    pthread_create() is %d\n", rc);
                          exit(-1);
                }
      }

      for(t=0;t<NUMBER_OF_THREADS;t++) {
                pthread_join(threads[t],NULL); 
      }

      pthread_exit(NULL); // this takes an input parameter !

     }

I was thinking that it was a problem with MALLOC (because it doesn't initialize memory allocated). So I used CALLOC , but the same problem occured.

Upvotes: 3

Views: 2073

Answers (2)

user2371524
user2371524

Reputation:

            rc = pthread_create(&threads[t], NULL, PrintHello, 
                       (void  *) mystArray);

You're missing the array index here, you mean mystArray + t.

On a side note: remove all these casts, they make the code hard to read. void * is generic and implicitly convertible in .

Upvotes: 4

Andrew Henle
Andrew Henle

Reputation: 1

This line is wrong:

            rc = pthread_create(&threads[t], NULL, PrintHello, 
                       (void  *) mystArray);

Given how you're written the rest of the code, it should be:

            rc = pthread_create(&threads[t], NULL, PrintHello, 
                       (void  *) ( mystArray[t] ) );

But the use of malloc()/calloc() is unnecessary:

       myst mystArray[ NUMBER_OF_THREADS ];
       memset( mystArray, 0, sizeof( mystArray ) );
       ...
            rc = pthread_create(&threads[t], NULL, PrintHello, 
                       (void  *) &( mystArray[t] ) );

That will work without the malloc()/calloc().

Upvotes: 2

Related Questions