【发布时间】:2016-06-30 12:37:11
【问题描述】:
我的这段代码如下所示。
其中 nrthr 代表线程数,由用户输入。我总是希望我的“主程序”调用 testFunc 一次,所以如果用户输入 nrthr 为数字 3,那么我想创建 2 个新线程。所以我的问题是......我怎样才能在 while 循环之前调用 testFunc?
int threadCount = 0;
...
// Call testFunc here
while(threadCount < nrthr - 1) {
fprintf(stderr, "Created thread: %d\n", threadCount);
if(pthread_create(&(tid[threadCount++]), NULL, testFunc, args) != 0)
fprintf(stderr, "Can't create thread\n");
}
void *testFunc(void *arg)
{
...
}
【问题讨论】:
-
void* pin, *pout; pout = testFunc(pin);之类的有什么问题? -
是什么阻止您将其作为普通函数调用 void *ret = testFunc(somedata/NULL)?
-
我想我应该这样做,我只是想我可以在不创建变量的情况下调用它,因为该函数不返回任何内容。
-
@Fjodor,当然,如果你不关心返回值,你不需要将它存储在一个变量中,你可以调用你的函数:
testFunc(NULL),或者其他您要传递给函数的值
标签: c void-pointers