-4

I have a structure named graph.

graph *G;

Now I create memory of size of structure using malloc and the start pointer is stored in pointer G.

G = (graph*)malloc(sizeof(graph));

My question is how do I pass this as a argument to pthread_create?

pthread_create(&th,NULL,function_call, ? );

closed as off-topic by yannis Mar 18 '15 at 7:45

  • This question does not appear to be about software engineering within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Are you sure you don't mean C rather than C++? – outis Mar 18 '15 at 5:30
  • 1
    Also, this is more of an implementation question than a design question, so should be asked on SO. – outis Mar 18 '15 at 5:35
  • Ya but then since I have posted it here already.. Why not answer it? – Nandini Singhal Mar 18 '15 at 5:39
  • just like in a previous question, I wonder, are you by chance blocked at Stack Overflow – gnat Mar 18 '15 at 7:15
  • 1
    This question does not appear to be about software development within the scope defined in the help center. – yannis Mar 18 '15 at 7:45
-1

the prototype of pthread_create is

 int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);

As the arg parameter is a void* you can simply write

pthread_create(&th,NULL,function_call, G);

However, it the function_call(), you will need to cast you arg parameter to the real type. either:

function_call(void *arg)
{
    graph *tmp_G;

    tmp_G = arg;
    //  then I use tmp_G as G
}

or

((graph *)arg)->the_field_of_G;

Then you will be able to use you parameter as you need.

If you want to give more then one parameter to you function, you simply have to create a new structure that will contain all your parameters, then cast it as above to use them in your function_call().

here is an example :

typedef struct s_my_function_call_arg
{
    graph *G;
    int   arg2;
    char  *arg3;
}              t_my_function_call_arg;
  • ssca2.cpp: In function ‘int main(int, char**)’: ssca2.cpp:169:63: error: invalid conversion from ‘void ()(void)’ to ‘void* ()(void)’ [-fpermissive] rc = pthread_create(&threads[count], NULL,genScalData,G); ^ In file included from ssca2.cpp:79:0: /usr/include/pthread.h:244:12: error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* ()(void), void*)’ [-fpermissive] extern int pthread_create (pthread_t *__restrict __newthread, ^ – Nandini Singhal Mar 18 '15 at 9:18
  • The answer is writtend in your compilation error messages : invalid conversion from ‘void ()(void)’ to ‘void* ()(void). The return value of genScalData is suppose to be void *. – Bernard Jesop Mar 18 '15 at 9:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.