Alessandroempire
Alessandroempire

Reputation: 1699

c passing several arguments to threads

when i create a thread, i want to pass several arguments. So i define in a header file the following:

struct data{
  char *palabra;
  char *directorio;
  FILE *fd;
  DIR *diro;
  struct dirent *strdir;

};

In a .c file i do the following

if (pthread_create ( &thread_id[i], NULL, &hilos_hijos, ??? ) != 0){
       perror("Error al crear el hilo. \n");
       exit(EXIT_FAILURE);
} 

How do i pass all this arguments to the threads. I though about:

1) first use malloc to allocate memory for this structure and then give each parameter a value:

 struct data *info
 info = malloc(sizeof(struct data));
 info->palabra = ...;

2) define

 struct data info 
 info.palabra = ... ; 
 info.directorio = ...; 

and then, how do i access these parameters in the thread void thread_function ( void *arguments){ ??? }

thanks in advance

Upvotes: 2

Views: 9800

Answers (4)

Sean Bright
Sean Bright

Reputation: 120714

Here is a working (and relatively small) example:

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

/*                                                                                                                                  
 * To compile:                                                                                                                      
 *     cc thread.c -o thread-test -lpthread                                                                                         
 */

struct info {
    char first_name[64];
    char last_name[64];
};

void *thread_worker(void *data)
{
    int i;
    struct info *info = data;

    for (i = 0; i < 100; i++) {
        printf("Hello, %s %s!\n", info->first_name, info->last_name);
    }
}

int main(int argc, char **argv)
{
    pthread_t thread_id;
    struct info *info = malloc(sizeof(struct info));

    strcpy(info->first_name, "Sean");
    strcpy(info->last_name, "Bright");

    if (pthread_create(&thread_id, NULL, thread_worker, info)) {
        fprintf(stderr, "No threads for you.\n");
        return 1;
    }

    pthread_join(thread_id, NULL);

    return 0;
}

Upvotes: 7

Vishwanath Sungal
Vishwanath Sungal

Reputation: 131

1) you need to use malloc and not define like below

struct data *info;
info = (struct data *)malloc(sizeof(struct data));

and pass the pointer of the structure in ptherad call as below

pthread_create ( &thread_id[i], NULL, &thread_fn, (void *)info );

2) you can access them in thread function as below

void thread_function ( void *arguments){

struct data *info = (struct data *)arguments;

info->....

}

Upvotes: 0

Hunter McMillen
Hunter McMillen

Reputation: 61550

Create a pointer to a struct like you do in the first case above:

//create a pointer to a struct of type data and allocate memory to it
struct data *info
info = malloc(sizeof(struct data));

//set its fields
info->palabra   = ...;
info->directoro = ...;

//call pthread_create casting the struct to a `void *`
pthread_create ( &thread_id[i], NULL, &hilos_hijos, (void *)data);

Upvotes: 0

Keith Randall
Keith Randall

Reputation: 23265

Do not use option #2. The data structure could be overwritten (explicitly, for instance using the same structure to start another thread, or implicitly, for instance having it overwritten on the stack). Use option #1.

To get at your data, at the start of your thread, do

struct data *info = (struct data*)arguments;

Then access info as normal. Make sure to free it when the thread is done (or, as I prefer, have the caller free it after joining with the thread).

Upvotes: 0

Related Questions