tags:

views:

144

answers:

3

I have created a function for a thread, but I want to pass multiple parameters to the function.

Here's my source code :

#include "work.h"
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>    // compile with -lpthread

int count = 20;

void* ChildProc(void* arg)
{
    int i;

    for(i = 1; i <= count; i++)
    {   
        printf("%s:%d from thread <%x>\n", arg, i, pthread_self());
        DoWork(i);
    }

    return NULL;
}

void ParentProc(void)
{
    int i;

    for(i = count / 2; i > 0; i--)
    {
        printf("Parent:%d from thread <%x>\n", i, pthread_self());
        DoWork(i);
    }
}

int main(void)
{
    pthread_t child;

    pthread_create(&child, NULL, ChildProc, "Child");

    ParentProc();

    pthread_join(child, NULL); // make child a non-daemon(foreground) thread
}

Now how do I pass multiple parameter to ChildProc() method?

One way is either pass an array or a structure. But what if I want to pass multiple variables without an array or a structure?

+5  A: 

A fast and junk answer is to create a struct to hold all parameter and pass its pointer

mmonem
How do I create parameter class object in C?
Searock
Forget **class object**. in C: Create a struct to hold parameters, instantiate a variable from it, set the parameters, pass the pointer of this variable over
mmonem
There's nothing junk about the fast answer - it is the correct answer.
Jonathan Leffler
+3  A: 

One way is either pass a array or a structure.

That's the way. Pointer to a structure, that is.

what if I want to pass multiple variables withous a array or a structure?

Then you're out of luck. Array or a pointer to a structure is what you need.

Johann Gerell
@Johann Gerell Is there any other method similar to pthread_create() which can support multiple parameters?
Searock
Pass a pointer to a structure, not a structure per se.
Jonathan Leffler
@Searock: no - there isn't any other method similar to pthread_create() which supports multiple parameters.
Jonathan Leffler
+2  A: 

You can pass a void * buffer stream and if you know the lengths then you can access them. Its similar to implementation of GArrays.

How do you create them?

void *buffer = malloc(sizeofelements);
memcpy(buffer,element1,sizeof element1);
memcpy(buffer+sizeof element1,element2, sizeof element2);

NOTE: The above is not a compilable C code. You need to work on it.

You can use something of the above sort.

You can later access the variables as you already know the size

Praveen S
What do you mean by "is not a compilable C code" ?
Searock
I meant it wont compile if you CTRL-C CTRL-V, However if you use the code and the function calls correctly it will work. Like`memcpy(dest,ptr,size);` and what i have written is enough to understand one has to do as stated here.
Praveen S
+1 for providing an alternative way.
Searock
-1 This is exactly the same way as passing a pointer to a struct except that it's not as type safe and is not as readable.
JeremyP
@JeremyP - You forgot to add without actually declaring the structure in your comment. I agree with your other points but the OP asked for an alternative.
Praveen S
@Praveen S: My point is that this isn't really an alternative. It's doing exactly the same thing as passing a struct but without explicitly defining the structure of the block of memory.
JeremyP