Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8119453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:48:31+00:00 2026-06-06T04:48:31+00:00

when i create a thread, i want to pass several arguments. So i define

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:48:33+00:00Added an answer on June 6, 2026 at 4:48 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am want to pass data back from a Thread to Activity (which created
I want to create a thread and then pass parameters to it. But I
I want to create a Thread safe JSP page. It is possible in Servlet
I want to create a background thread that's owned by an object. When that
I want to create a separate thread that runs its own window. Frankly, the
I want to create a worker thread that isn't the main thread so I
I want to create dynamic number of thread which is depends on the database
I've added the following Extensions to my Project in order to create a thread
I want to pass an exception from current thread (that thread isn't main thread)to
If I want to pass a char* to various threads via pthread_create() and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.