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?
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?
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