13

I am trying to pass two parameters to a thread in C. I have created an array (of size 2) and am trying to pass that array into the thread. Is this the right approach of passing multiple parameters into a thread ?

// parameters of input. These are two random numbers 
int track_no = rand()%15; // getting the track number for the thread
int number = rand()%20 + 1; // this represents the work that needs to be done
int *parameters[2];
parameters[0]=track_no;
parameters[1]=number;

// the thread is created here 
pthread_t server_thread;
int server_thread_status;
//somehow pass two parameters into the thread
server_thread_status = pthread_create(&server_thread, NULL, disk_access, parameters);
alk
  • 69,737
  • 10
  • 105
  • 255
Ashish Agarwal
  • 14,555
  • 31
  • 86
  • 125
  • 1
    Check your code you are declaring an array of pointers to int and assigning them with int values. – Teudimundo Nov 22 '11 at 08:20
  • I did notice that warning. Would it be legitimate if parameters is not pointers and simply an array ? – Ashish Agarwal Nov 22 '11 at 08:23
  • if you declare parameter to be an array of int ("int parameter[2];"), then you can pass parameter as a pointer. It is the pointer to first int. You can then access it from the thread as an array. – Teudimundo Nov 22 '11 at 08:27

2 Answers2

18

Since you pass in a void pointer, it can point to anything, including a structure, as per the following example:

typedef struct s_xyzzy {
    int num;
    char name[20];
    float secret;
} xyzzy;

xyzzy plugh;
plugh.num = 42;
strcpy (plugh.name, "paxdiablo");
plugh.secret = 3.141592653589;

status = pthread_create (&server_thread, NULL, disk_access, &plugh);
// pthread_join down here somewhere to ensure plugh
//   stay in scope while server_thread is using it.
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 5
    Of course, in code as shown in the example above, you have to make sure that the structure isn't destroyed while the thread attempts to dereference the given opinter. – Frerich Raabe Nov 22 '11 at 08:30
  • The easiest fix is to `malloc` the structure and make the new thread responsible for freeing it. Another approach is to put a barrier inside the structure and have both the original and new thread wait on the barrier before the original thread returns. – R.. GitHub STOP HELPING ICE Nov 22 '11 at 08:53
  • @FrerichRaabe Maybe a stupid question, but how does the structure "get destroyed"... other than a call to `free()` (if it was `malloc`ed) or the function returning (if it was allocated on the stack), is there any other way? – The111 May 27 '12 at 06:36
  • 1
    @The11, I think what Frerich may be alluding to is _reuse._ Some people will populate the structure then pass it to a thread, then populate the _same_ structure for another thread, not realising that the first thread may not yet have made a local copy. This could be solved by arrays of structures, one per thread, or by other inter-thread mechanisms, like a mutex-protected "thread has made local copy of its information" flag. – paxdiablo May 27 '12 at 06:44
  • @paxdiablo - shouldn't we cast pthread_create argument to void*? (&plug) i mean why it accept any type when the prototype declare that it must be void*. – muradin Dec 14 '13 at 13:45
  • 1
    @muradin, C will cast to/from `void*` implicitly. You don't need to do it explicitly. – paxdiablo Dec 14 '13 at 21:55
1

That's one way. The other usual one is to pass a pointer to a struct. This way you can have different "parameter" types, and the parameters are named rather than indexed which can make the code a bit easier to read/follow sometimes.

Mat
  • 202,337
  • 40
  • 393
  • 406