我为线程创建了一个函数,但是我想将多个参数传递给该函数。如何将多个参数传递给线程函数
这里是我的源代码:
#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
}
现在我该怎样传递多个参数ChildProc()方法?
一种方法是传递数组或结构。但是如果我想传递多个变量而没有数组或结构呢?
是否有任何其他类似于pthread_create()的方法可以支持多个参数? – Searock 2010-09-09 05:59:27