简体   繁体   English

C中的pthread_create参数

[英]pthread_create arguments in C

Looking at the man page for pthread_create(...) , I see the definition is as follows... 看一下pthread_create(...)的手册页,我看到定义如下...

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);

My first question is in pthread_create while passing argument 3; 我的第一个问题是在传递参数3时在pthread_create am I casting the address of function name or just the function name? 我要转换函数名称的地址还是仅转换函数名称? For me it seems to produce the same results... so what is the difference between these? 对我来说,似乎产生了相同的结果...那么这些之间有什么区别?

(void *)&function_name
(void *)function_name

My second question is in pthread_create while passing argument 4, should we always give the address of the args variable like, 我的第二个问题是在传递参数4时在pthread_create中,是否应该始终像这样给出args变量的地址,

(void *) &variable

or can we directly pass the value like 还是我们可以像这样直接传递值

(void *)variable

Thanks. 谢谢。

Continued from the comments above... 继续以上评论...

(1) The pthread_create start function (3rd parameter) must have a signature of (1) pthread_create启动函数(第3个参数)的签名必须

void *(*start_routine) (void *)

that is, a function that takes a void pointer and returns a void pointer. 也就是说,该函数需要一个void指针并返回一个void指针。 For example: 例如:

void* myfunc(void *)

You are getting compile errors because your start function probably (and incorrectly) looks like this: 您收到编译错误,因为您的启动功能可能(并且错误地)如下所示:

void myfunc(void)

in other words a function that takes no parameters and returns no result. 换句话说,一个不带参数且不返回结果的函数。 You got around this compiler error by casting myfunc to a pointer to void but it is unnecessary and wrong. 通过将myfunc强制转换为void指针,可以解决此编译器错误,但这是不必要且错误的。 Your real problem is that your start function "myfunc" has the wrong signature. 您真正的问题是启动函数“ myfunc”的签名错误。

(2) It seems you are hung up on void and pointers to void. (2)似乎您迷上了void和指向void的指针。 There several good answers here on this including this . 这里有一些很好的答案,包括this Basically a pointer to void is a generic pointer that can point to any type. 基本上,指向void的指针是可以指向任何类型的通用指针。

pthread_create is itself an excellent example of the use of void ptrs. pthread_create本身就是使用void ptrs的绝佳示例。 Because there is no way for a start function to know what kind of data a developer wants to pass into (and return) from the function they use a void ptr that can point to any type. 因为启动函数无法知道开发人员希望从该函数传递(或返回)什么样的数据,所以他们使用可以指向任何类型的void ptr。 It is up to the developer of the start function to then cast the void ptr to appropriate type he actually passed before using whatever the void ptr points to. 然后由start函数的开发人员将void ptr转换为他实际使用的适当类型,然后再使用void ptr指向的任何内容。 So now the start function can process a ptr that may in actually point to an int, a double, an array, a structure, or whatever. 因此,现在开始函数可以处理一个ptr,它实际上可以指向一个int,一个double,一个数组,一个结构或其他任何东西。

In your case you are turning a pointer to a function with a specific signature into a pointer to anything. 在您的情况下,您正在将具有特定签名的函数的指针转换为任何内容的指针。 This may satisfy the compiler because it assumes you know what you are doing. 这可能会使编译器满意,因为它假定您知道自己在做什么。 But in this instance you don't because you are papering over the real error which is that the signature of your start function is wrong. 但是在这种情况下,您不会这样做是因为您正在解决真正的错误,即启动功能的签名是错误的。

Function and array address will be get with or without &. 函数和数组地址将带有或不带有&来获取。
But other varibles won't, you should still use (void*)&variable. 但是其他变量不会,您仍然应该使用(void *)&variable。

FYI 费耶
Function pointers and address of a function 函数指针和函数地址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM