Passing arguments to thread in C

  • Thread starter James889
  • Start date
  • Tags
    Thread
In summary, the conversation discusses an issue with passing arguments to a thread via a struct. The OP realizes their mistake of not correctly initializing the struct and not properly passing arguments to the thread. They also discuss the lack of output from the thread routine due to a mistake in the function call. The conversation ends with a clarification that it is not a homework assignment.
  • #1
James889
192
1
Hi,

I am trying to figure out how to pass arguments to a thread via a struct.
It's not really working out for me.

What am i doing wrong here?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>


void *run(void *thread_arg);

struct thread_args {
  
      int arraylength;
      int *ptr;
      char *name;
};
        
int main() {
        
  pthread_t thread_id;
  struct thread_args *thread_data;
 

  int array[10] = { 1,2,3,4,5,6,7,8,9,10 };
  
  
  thread_data->arraylength = 10;
  thread_data->ptr = &array[0];
 
  strcpy(thread_data->name,"David"); /* Is this correct ? */
  printf("%s",thread_data->name); //This prints nothing

  
  pthread_create(&thread_id,NULL,run,(void *)&thread_data);
  pthread_join(thread_id,NULL);
  return 0;
  }    
       

 void *run(void *thread_arg) {
        printf("hi");
        struct thread_args *my_data;
        my_data = (struct thread_args *)thread_arg;
        printf("%s",my_data->name);
         
         
  }
 
Technology news on Phys.org
  • #2
You declared an instance of a pointer to struct thread_data, but you never declared or allocated an instance of struct thread_data. Looking at your code I think you just need to remove the pointer reference:

Code:
    struct thread_args thread_data;
    ...  
    thread_data.arraylength = 10;
    thread_data.ptr = &array[0];

or if you want to use a pointer

Code:
    struct thread_args thread_data;
    struct thread_args *pthread_data = &thread_data;
    
    ...  
    pthread_data->arraylength = 10;
    pthread_data->ptr = &array[0];
    ...
    pthread_create(&thread_id,NULL,run,(void *)pthread_data);
 
Last edited:
  • #3
Oops, stupid mistake :redface:

But why is there no output from the thread routine?

It is supposed to print the name "David"
 
Last edited:
  • #4
James889 said:
Oops, stupid mistake :redface:

But why is there no output from the thread routine?

It is supposed to print the name "David"

I'm not familiar with the syntax of p_threads, but if you passed a bad pointer as a result of your (thread_data) variable, then the thread has most likely not been created at the very least due to a pointer problem and the thread won't have been allocated, let alone run.

Assuming there are no problems in this regard (and the program didn't crash), then the only thing I could think of is that you didn't make the right call or pass the right argument for the thread to automatically start.

To be honest though, I'm surprised you didn't get any crashes due to your bad code that doesn't even instantiate or allocate the data for the struct you are using.
 
  • #5
This looks like homework.
Is it?
 
  • #6
James889 said:
But why is there no output from the thread routine?
Because you added yet another level of indirection in the function call. Thread_data pointer was allocated on the stack, but probably not initialized or initialized to zero, so you just stored the strings into some random location in memory or location zero. Then you called the function with &thread_data, which is the address within the stack for the thread_data pointer.
 
  • #7
I like Serena said:
This looks like homework.
Is it?

No, it's not. It's home work, play for fun :)
 

1. How do you pass arguments to a thread in C?

In C, arguments can be passed to a thread using the pthread_create() function. This function takes in four arguments, the first being a pointer to a pthread_t variable which will store the thread ID, the second being a pointer to the pthread_attr_t structure which determines the thread's attributes, the third being a pointer to the function the thread will execute, and the fourth being a pointer to the arguments to be passed to the function.

2. Can I pass multiple arguments to a thread in C?

Yes, you can pass multiple arguments to a thread in C by creating a struct that contains all the necessary arguments and passing a pointer to this struct as the fourth argument in the pthread_create() function.

3. How do you access the passed arguments inside a thread function in C?

In order to access the passed arguments inside a thread function in C, you can use a typecast to convert the void* argument to the desired data type. For example, if you passed an integer as an argument, you can use *(int*)arg to access the value inside the thread function.

4. Is it necessary to pass arguments to a thread in C?

No, it is not necessary to pass arguments to a thread in C. If your thread function does not require any arguments, you can simply pass NULL as the fourth argument in the pthread_create() function.

5. Are the passed arguments in a thread function in C passed by value or by reference?

In C, the passed arguments in a thread function are passed by value, which means that a copy of the argument is made and passed to the thread function. Any changes made to the arguments inside the thread function will not affect the original values in the main program.

Similar threads

  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
895
Back
Top