1

So I have this

void* tf(void* p);

which I dont totally understand. What I think it is, is a function pointer with a void pointer for a parameter. I am using it to make a thread like this:

pthread_create( &thread_id[i], NULL, tf, NULL );

What I need is an expliation of tf and how to pass a parameter to it.

I have the function defined as

void* tf(void* p) 
{
    //I would like to use p here but dont know how. 
}

This function is outside the main and needs to get a few other parameters that are set inside main. I have tried making it look like this tf(int i) but I then get a segment fault. So I know I am doing something wrong and need some help figuring it out.

Thanks for any help in this mater.

Jason

Xintaris
  • 35
  • 1
  • 10

4 Answers4

1
pthread_create( &thread_id[i], NULL, tf, NULL );
//                                      ^^^^^
//                       You have to put here the pointer (address) to your data

and then you can get your data from the p pointer into the thread function

Example

typedef struct test {
   int a,b;
} test;

int main() 
{
   struct test T = {0};
   pthread_create( &thread_id[i], NULL, tf, &T );
   pthread_join(thread_id[i], NULL); // waiting the thread finish
   printf("%d  %d\n",T.a, T.b);
}

void* tf(void* p) 
{
    struct test *t =  (struct test *) p;
    t->a = 5;
    t->b = 4;
}
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
0

The last parameter to pthread_create is passed to the thread function.

So in your case, define what you want to pass to thread function as pass its address in thread create function. Like

//in main
char *s = strdup("Some string");
pthread_create( &thread_id[i], NULL, tf, s );
...

void* tf(void* p) 
{
    char *s = (char *) p;
    // now you can access s here.

}
Rohan
  • 52,392
  • 12
  • 90
  • 87
0

The void argument to the callback function can be a pointer to anything you want it to be or to nothing at all (NULL). You just have to cast it correctly. The tf function, itself, is the function that is expected to do the work of the child thread and if you want it to return some kind of value on exit, then you return that as a pointer just like you did when you passed arguments starting the thread.

struct settings
{
    int i;
    int j;
    char* str;
};

void* tf( void* args )
{
    int result = 0;

    struct settings* st = (struct settings*)args;

    // do the thread work...

    return &result;
}

int main( int argc, char** argv )
{
    struct settings st;

    st.i = atoi( argv[1] );
    st.j = atoi( argv[2] );
    st.str = argv[3];

    pthread_create( &thread_id[i], NULL, tf, &st );

    // do main work...
}
K Scott Piel
  • 4,320
  • 14
  • 19
0

tf is just a function that receives a void * argument and returns a void * value at the end. No function pointers so far.

pthread_create needs a pointer to a function to execute, so instead of passing tf, you need to pass &tf to pass in tf's location.

void * in C is just a completely generic pointer. So you cast it to whatever you are passing in, thus:

int a = 4;
void * a_genptr = (void *)&a;
pthread_create(... &tf ... a_genptr ...);

void * tf(void * arg) {
    int thatIntIWanted = *(int *)arg; // cast void * to int *, and dereference
}

You can see an example of that in this other SO question

Community
  • 1
  • 1
Phil H
  • 19,928
  • 7
  • 68
  • 105
  • Thanks for the explanations. These will definitely help me resolve the issue. I will try this out when I have some time. – Xintaris May 09 '13 at 20:50