Reputation: 2043
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?
Upvotes: 6
Views: 4086
Reputation: 25591
The declaration of CreateThread
says that the first parameter pfn
is a pointer to a callback function that will be used by CreateThread
. The callback pfn
is your own function that CreateThread
will call so that you can execute your code in a new thread.
You define a function
void MyThreadCallback(void* data)
{
MyData myData = reinterpret_cast<MyData*>(data);
...
}
and pass it to CreateThread
as
MyData* myData = new MyData();
CreateThread(MyThreadCallback, myData, ... );
so that CreateThread
can call it in the context of the new thread.
Upvotes: 2
Reputation: 65516
This is a function pointer (or a pointer to a function).
void(*pfn)(void*)
This is broken down as such:
*pfn
(the name of the pointer i.e. pointer to a function)
(void *)
(these are the parameters to the function ie. a simple pointer to anything)
void
(this is return from the function)
So if you have a function like this:
void DoSomeThing(void *data) {
... does something....
}
then you can pass it into the CreateThread
function like so...
int i = 99;
void * arg = (void*)&i;
pthread_t thread = CreateThread(DoSomeThing, arg, ... other parameters ...);
So somewhere in CreateThread
it will make a call:
pfn(parg);
and your function DoSomeThing will be called and void * data
you get will be the arg you passed in.
More info:
Remember that code is just a sequence of bytes in memory. It's just how the cpu interprets them that makes them different from the thing we call data.
So at any point in a program we can refer to another part of the code by it's memory address. 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.
In the above example the CreateThread function needs the address of a function so it can execute that function in a new thread. So we pass it a pointer to that function. Hence we pass it a function pointer.
Upvotes: 7
Reputation: 234847
These things are easiest read inside out:
(*pfn)
=> "pfn
is a pointer"void x(void*)
=> "x
is a function accepting a void *
argument and returning void
"Put them together and you have:
pfn
is a pointer to a function accepting a void *
argument and returning void
"Note that the parens around (*pfn)
are necessary because of precedence. 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 *
".
Upvotes: 8
Reputation: 18964
It is a function pointer. The name pfn is a clue. Specifically it is a pointer to a function that takes a "void pointer" and returns nothing. 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. Probably more detail than necessary, since you can write perfectly good C++ code without ever using either. If your books are somehow all super modern and don't cover them, look for a C reference.
Upvotes: 1
Reputation: 7490
This is a function pointer returning nothing and taking a void pointer (see the section called void pointers).
Upvotes: 2
Reputation: 34625
pfn
is a function pointer whose return type is void and argument's type is void*
Upvotes: 1
Reputation: 182664
It's a function pointer to a function returning void
and accepting void *
.
void example(void *arg);
You can find more information about function pointers in C++ (and in C) at The Function Pointer Tutorials.
Upvotes: 9
Reputation: 887767
This is called a function pointer.
It points to a function rather than a variable.
If it isn't in your book, you should get a better book.
Upvotes: 5