【发布时间】:2010-09-09 05:49:59
【问题描述】:
我已经为一个线程创建了一个函数,但是我想给函数传递多个参数。
这是我的源代码:
#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()的方法可以支持多参数吗?