Passing arguments to thread in C

  • Thread starter Thread starter James889
  • Start date Start date
  • Tags Tags
    Thread
Click For Summary

Discussion Overview

The discussion revolves around the challenges of passing arguments to a thread in C using a struct. Participants explore issues related to memory allocation, pointer usage, and output from the thread routine.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant expresses difficulty in passing arguments to a thread and questions the correctness of their code, particularly regarding the use of a struct.
  • Another participant points out that the original poster declared a pointer to the struct without allocating memory for it, suggesting they should either use a direct instance or properly allocate memory for the pointer.
  • A participant questions why there is no output from the thread routine, speculating that a bad pointer may prevent the thread from being created or run.
  • Further clarification is provided that the pointer may not have been initialized, leading to potential issues with storing strings in memory.
  • There is a meta-discussion about whether the original post is homework, with one participant asserting it is not, while another suggests it appears to be homework.

Areas of Agreement / Disagreement

Participants generally agree that there are issues with memory allocation and pointer usage, but there is no consensus on the exact cause of the lack of output from the thread routine. The discussion remains unresolved regarding the specific implementation details and their implications.

Contextual Notes

Limitations include the lack of memory allocation for the struct pointer and potential issues with pointer initialization, which may affect the program's behavior.

James889
Messages
190
Reaction score
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
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:
Oops, stupid mistake :redface:

But why is there no output from the thread routine?

It is supposed to print the name "David"
 
Last edited:
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.
 
This looks like homework.
Is it?
 
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.
 
I like Serena said:
This looks like homework.
Is it?

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

Similar threads

Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 13 ·
Replies
13
Views
21K