我正在学习C语言,但我的书提供的资源非常少。我想知道是否有可能,并且使用该线程来详细说明某些内容并将结果传递给main是有用的。在这种情况下,我想知道如何将信息从一个线程传递给main(类似于java中的方法return)。唯一的解决办法是使用全局变量? 这里有一个小例子来解释我想要做什么。这可能/有用吗?C - 主线程结果
主:
int i = 1;
pthread_create(tid,NULL,functionTH,NULL);
int z = //getResultFromThread
线 “功能”:
void * functionTH(){
int z = 2;
//return value 2 to the main and assign it to k in the main.
}
毫米,但value_ptr应该是在pthread_join和了pthread_exit空隙都,所以我怎么能传递一个INT throught呢? – testermaster 2014-10-08 18:26:13
您可以投射'(void *)'例如'struct'的指针,它将包含您的'i'和'k'。 – soerium 2014-10-08 18:27:14
那么,这样的事情应该工作?'void * functionTH(){pthread_exit((void *)22);}}}; (void *)&k); printf(“%d”,k);' – testermaster 2014-10-08 18:34:39