简体   繁体   English

有人可以解释参数 void(*pfn)(void*) 吗?

[英]Can someone explain the parameter void(*pfn)(void*)?

I've come across this piece of code我遇到了这段代码

inline pthread_t CreateThread(void(*pfn)(void*), void* parg, bool fWantHandle=false)

I don't understand this part我不明白这部分

void(*pfn)(void*)

Can someone tell me what it means/is?有人能告诉我这是什么意思吗?

This is btw not listed in books for beginners so if you want to mention to read books, it's not there.顺便说一句,这在初学者书籍中没有列出,所以如果你想提及阅读书籍,它不在那里。

Afaik, void is datatype of a function meaning it will not return anything, however that part there...void is used on a pointer? Afaik,void 是 function 的数据类型,这意味着它不会返回任何内容,但是那里的那部分...... void 用于指针?

It's a function pointer to a function returning void and accepting void * .这是一个 function 指针,指向一个 function 返回void并接受void *

void example(void *arg);

You can find more information about function pointers in C++ (and in C) at The Function Pointer Tutorials .您可以在Function 指针教程的C++(和 C 语言)中找到有关 function 指针的更多信息。

These things are easiest read inside out:这些东西最容易从里到外阅读:

  • (*pfn) => " pfn is a pointer" (*pfn) => " pfn是一个指针"
  • void x(void*) => " x is a function accepting a void * argument and returning void " void x(void*) => " x是一个 function 接受一个void *参数并返回void "

Put them together and you have:将它们放在一起,您将拥有:

  • " pfn is a pointer to a function accepting a void * argument and returning void " “ pfn 是指向pfn的指针,接受void *参数并返回void

Note that the parens around (*pfn) are necessary because of precedence.请注意,由于优先级, (*pfn)周围的括号是必需的。 void *pfn(void*) would be interpreted as if it were written (void *)pfn(void *) : " pfn is a function that accepts a void * and returns a void * ". void *pfn(void*)将被解释为好像写成(void *)pfn(void *) :“ pfn是一个接受void *并返回void *的 function ”。

This is a function pointer (or a pointer to a function).这是一个 function 指针(或指向函数的指针)。

void(*pfn)(void*)

This is broken down as such:这是这样分解的:

*pfn (the name of the pointer ie pointer to a function) *pfn (指针的名称,即指向函数的指针)

(void *) (these are the parameters to the function ie. a simple pointer to anything) (void *) (这些是 function 的参数,即指向任何东西的简单指针)

void (this is return from the function) void (这是从函数返回)

So if you have a function like this:因此,如果您有这样的 function:

void DoSomeThing(void *data) {
  ... does something....
}

then you can pass it into the CreateThread function like so...然后你可以像这样将它传递给CreateThread function ...

int i  = 99;
void * arg = (void*)&i;
pthread_t thread = CreateThread(DoSomeThing, arg, ... other parameters ...);

So somewhere in CreateThread it will make a call:所以在CreateThread的某个地方它会调用:

pfn(parg);

and your function DoSomeThing will be called and void * data you get will be the arg you passed in.并且您的 function DoSomeThing 将被调用,您获得的void * data将是您传入的参数。

More info:更多信息:

Remember that code is just a sequence of bytes in memory.请记住,代码只是 memory 中的一个字节序列。 It's just how the cpu interprets them that makes them different from the thing we call data.正是 cpu 对它们的解释方式使它们与我们所说的数据不同。

So at any point in a program we can refer to another part of the code by it's memory address.因此,在程序中的任何时候,我们都可以通过 memory 地址来引用代码的另一部分。 Since the code is broken down into functions with in C, this is a useful unit of reuse that C understands and allows us to treat the address of the function as just another pointer to some data.由于代码在 C 中分解为函数,因此这是一个有用的重用单元,C 可以理解并允许我们将 ZC1C425268E68385D1AB5074C17A94F14 的地址视为另一个指向数据的指针。

In the above example the CreateThread function needs the address of a function so it can execute that function in a new thread.在上面的示例中,CreateThread function 需要 function 的地址,因此它可以在新线程中执行 function。 So we pass it a pointer to that function.所以我们将一个指向 function 的指针传递给它。 Hence we pass it a function pointer .因此我们传递给它一个function 指针

This is called a function pointer .这称为function 指针
It points to a function rather than a variable.它指向 function 而不是变量。

If it isn't in your book, you should get a better book.如果它不在你的书中,你应该得到一本更好的书。

This is a function pointer returning nothing and taking a void pointer (see the section called void pointers ).这是一个function 指针,不返回任何内容并采用void 指针(参见名为void 指针的部分)。

The declaration of CreateThread says that the first parameter pfn is a pointer to a callback function that will be used by CreateThread . CreateThread的声明说第一个参数 pfn 是指向回调pfn的指针,它将由CreateThread使用。 The callback pfn is your own function that CreateThread will call so that you can execute your code in a new thread.回调 pfn 是您自己的pfnCreateThread将调用它,以便您可以在新线程中执行代码。

You define a function你定义一个 function

void MyThreadCallback(void* data)
{
    MyData myData = reinterpret_cast<MyData*>(data);
    ...
}

and pass it to CreateThread as并将其传递给CreateThread作为

MyData* myData = new MyData();
CreateThread(MyThreadCallback, myData, ... );

so that CreateThread can call it in the context of the new thread.以便CreateThread可以在新线程的上下文中调用它。

pfn is a function pointer whose return type is void and argument's type is void* pfn 是一个pfn指针,其返回类型为void ,参数类型为void*

It is a function pointer.它是一个 function 指针。 The name pfn is a clue. pfn 这个名字是一个线索。 Specifically it is a pointer to a function that takes a "void pointer" and returns nothing.具体来说,它是一个指向 function 的指针,它接受一个“空指针”并且什么都不返回。 A "void pointer" is a fake type that can be cast to some other kind of pointer. “空指针”是一种假类型,可以转换为其他类型的指针。

Most C++ books cover both void pointers and function pointers in some detail.大多数 C++ 书籍都详细介绍了 void 指针和 function 指针。 Probably more detail than necessary, since you can write perfectly good C++ code without ever using either.可能比必要的更详细,因为您可以编写非常好的 C++ 代码,而无需使用任何一个。 If your books are somehow all super modern and don't cover them, look for a C reference.如果您的书籍在某种程度上都是超现代的并且没有涵盖它们,请查找 C 参考。

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

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