这是一个来自Linux编程接口(原代码here)的程序。我所试图做的是使用pthread_create()下面列出的目标送2“论据”来的ThreadFunc:发送多个参数到pthread_create()
- 首先一个作为在ThreadFunc中的迭代器循环();
- 第二个标识当前正在threadFunc()中工作的线程。所以这将是某种可打印的线程ID。
为了实现这些目标,我创建这个结构,其中包含2个成员变量:
struct arguments {
int loops;
pthread_t self;
};
而这个函数循环“threadFuncLoops”次递增全局变量“水珠”
static void * threadFunc(void *arg)
{
struct arguments * threadFuncArgs = arg;
int threadFuncLoops = *(arg.loops);
for (int j = 0; j < threadFuncLoops; j++) {
// Something happens to glob
}
return NULL;
}
在主()我创建2个线程(t1,t2)并将它们发送到threadFunc():
struct arguments newArguments;
s = pthread_create(&t1, NULL, threadFunc, &newArguments);
s = pthread_create(&t2, NULL, threadFunc, &newArguments);
但是编译器ThreadFunc中说()
request for member 'loops' in something not a structure or union
我的问题是:
- 为什么 “循环” 不是一个结构?它在一个结构实例中是不是?
- 究竟如何实现目标#2?
非常感谢。
'*(arg.loops);'不正确。 'loop'不是一个指针,所以不能被指针化。此外,'args'是一个'void *',所以不是一个结构体指针或结构体。这在许多层面上都是错误的。它应该是'threadFuncArgs-> loops'。 – kaylum