我已经为线程创建了一个函数,但我想将多个参数传递给该函数。
这是我的源代码:
#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() 方法?
一种方法是传递数组或结构。但是如果我想在没有数组或结构的情况下传递多个变量怎么办?
最佳答案
一个快速而垃圾的答案是创建一个结构来保存所有参数并传递它的指针
关于c - 如何将多个参数传递给线程函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3674104/