Sign in to follow this  

passing data to thread functions?

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, im having trouble trying to figure out how to pass data to a function that is run by a thread. I figured i could pass in an array with all the data in it, but the problem is not all the data is of a uniform type, there are ints, chars, and strings. So i tried to create an array of void pointers that point to the data and pass the void pointer to the function, but im getting seg faults when i try and access the data. can anyone point me in the right direction? here is my code:
#include <stdio.h>
#include <pthread.h>

void * work( void * a )
{
  
  int i;
  for( i = 0; i < 11; i++ )
    {
      fprintf( stdout, "%d %d\n", a[1], i );
      usleep( 1 );
    }

  pthread_exit(0);
  return;
}


int main()
{
  pthread_t t1, t2;
  char string[5] = { 'h', 'e', 'l', 'l', 'o' };
  
  void * b[2];
  int x = 5;
  b[0] = string;
  b[1] = &x;

  pthread_create(&t1, 0, work, (void *)&b);
  pthread_create(&t2, 0, work, (void *)&b);

  printf( "HI\n" );
  pthread_join(t1, 0);
  pthread_join(t2, 0);
  printf( "END\n" );
  return 0;
}

Share this post


Link to post
Share on other sites
Create a struct to hold your data and pass a pointer to the struct as a parameter.


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


struct myData
{
char *string;
int x;
};

void * work( void * a )
{
myData *threadData = (myData *)a;
int i;
for( i = 0; i < 11; i++ )
{
fprintf( stdout, "%d %d\n", threadData->x,threadData->_string );
usleep( 1 );
}

pthread_exit(0);
return;
}


int main()
{
pthread_t t1, t2;

myData *threadData = new myData;
threadData->string = "hello";
threadData->x = 2;

pthread_create(&t1, 0, work, (void *)threadData);
pthread_create(&t2, 0, work, (void *)threadData);

printf( "HI\n" );
pthread_join(t1, 0);
pthread_join(t2, 0);
printf( "END\n" );
return 0;
}
[/soure]

Share this post


Link to post
Share on other sites
In the original code, methinks the problem is that you were looping over 11 array elements (0-10 inclusive), but only passing in an array of size 2... ^^

In the general case, though, passing a struct is a good idea, yeah.

Share this post


Link to post
Share on other sites
Actually, the problem with the original code is pointer confusion(which is why it is best to use a struct, it is so much cleaner)

An array of pointers is passed, but the function accepts a void* so you would have to use casting to access the actual elements. so,

fprintf( stdout, "%d %d\n", a[1], i );

becomes:

fprintf( stdout, "%d %d\n", *(((int**)a)[1]), i );

this will cast the a to a int** or an array of int pointers.

But anyways, use the struct. :)

Share this post


Link to post
Share on other sites

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this