pthread_create function format and pointers - C Linux POSIX library
我的问题是,就指针等而言,pthread_create函数及其调用的函数的格式到底是什么?尽管需要澄清我在该领域的知识,但是我可以将注意力集中在变量指针上,但是函数指针会变得很糟糕。
我了解首选格式是
1 2 3 4 5 6 7 8 | void *threadfunc(void *arg); int main() { pthread_t threadinfo; int samplearg; pthread_create(&threadinfo, NULL, threadfunc, &samplearg); } |
但是,这会生成一个编译器警告,提示threadfunc没有返回值,因此*显然是关于threadfunc返回的内容,不是该函数的特征吗?
我还看到了定义为的函数,pthread_create的格式如下:
1 2 3 | void threadfunc(void *arg); pthread_create(&threadinfo, NULL, (void *)threadfunc, &samplearg); |
这两个中的哪一个是正确的,或者在功能上等效?有人可以向我解释函数指针之类的机制吗?
最后一个问题,是否可以在for循环内生成多个线程,为线程唯一值初始化
转到"源"-POSIX标准。对于
1
2
3 int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);
也就是说,您的" start_routine"必须是一个返回
1 2 3 4 5 6 | void *possible_thread_start_routine(void *data) { SomeStruct *info = data; ...main code for thread... return 0; } |
传递给线程启动例程的参数是在
您尚未提供
顺便说一句,当另一个线程加入到您当前正在启动的线程中时,返回值将传递给pthread_join语句。
传递给
在线程退出后在线程上调用