Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing structures as arguments while using pthread_create()

I tried passing a structure as the 4th argument while using pthread_create() with something like this:

pthread_create(&tid1, NULL, calca, &t); //t is the struct

Now whenever I try to access variables in the structure - t.a, t.b or t.c, I keep getting an error - request for member in something not a structure or union.

What alternate method could I use to pass structs into the thread?

like image 479
skinderneath Avatar asked May 14 '09 15:05

skinderneath


People also ask

What are the arguments of pthread_create () call?

If thread is not NULL, pthread_create() stores the ID of the created thread in the location referenced by thread. At creation, the thread executes start, with arg as its sole argument. The calling function must ensure that arg remains valid for the new thread throughout its lifetime.

What is the last argument passed to the pthread_create () function?

The last parameter of pthread_create() is passed as the argument to the function, whereas the return value is passed using pthread_exit() and pthread_join() .

How do you pass arguments to Pthread function?

For cases where multiple arguments must be passed, this limitation is easily overcome by creating a structure which contains all of the arguments, and then passing a pointer to that structure in the pthread_create() routine. All arguments must be passed by reference and cast to (void *).

How do you pass a structure to a thread?

Your thread can access this struct by passing a pointer through the fourth argument of pthread_create() . Create a pointer to your struct as illustrated in the last exercise and do so. struct struct_name* arg_ptr = (struct struct_name*) args; printf("thread arg1: %d\n", arg_ptr->arg1);


2 Answers

You're probably creating the structure in the same scope as pthread_create. This structure will no longer be valid once that scope is exited.

Try creating a pointer to the structure on the heap and pass that structure pointer to your thread. Don't forget to delete that memory somewhere (in the thread if you'll never use it again - or when you no longer need it).

Also, as cyberconte mentioned, if you are going to be accessing that data from different threads, you'll need to lock access to it with a mutex or critical section.

Edit May 14th, 2009 @ 12:19 PM EST: Also, as other people have mentioned, you have to cast your parameter to the correct type.

If you are passing a variable that is a global structure (which you seem to be insisting upon), your thread function will have to cast to the type:

void my_thread_func(void* arg){
    my_struct foo = *((my_struct*)(arg)); /* Cast the void* to our struct type */
    /* Access foo.a, foo.b, foo.c, etc. here */
}

Or, if you are passing a pointer to your structure:

void my_thread_func(void* arg){
    my_struct* foo = (my_struct*)arg; /* Cast the void* to our struct type */
    /* Access foo->a, foo->b, foo->c, etc. here */
}
like image 197
Lyndsey Ferguson Avatar answered Oct 25 '22 17:10

Lyndsey Ferguson


If you're inside your thread function, the argument you pass is a void*. You'll need to cast it to a struct before you can use it as such.

void my_thread_func(void* arg){
    my_struct foo = (my_struct)(*arg); /* Cast the void* to our struct type */
    /* Access foo.a, foo.b, foo.c, etc. here */
}
like image 25
Harper Shelby Avatar answered Oct 25 '22 17:10

Harper Shelby