Connecting Tech Pros Worldwide Forums | Help | Site Map

Trouble with pointer to a function

Thomas Barth
Guest
 
Posts: n/a
#1: Oct 9 '05
Hi,
I dont understand the third argument of this function. This function
expect the address of a function, but what does "void *(*funktion)(void
*)," mean? I am confused by so many "wildcards" :-)

int pthread_create( pthread_t *thread,
const pthread_attr_t *attribute,
void *(*funktion)(void *),
void *argumente );


When invoking the pthread_create function I get an error at compile time.

[...]
void BlockOp::start() {
if(pthread_create(&pth, NULL, &BlockOp::process, cBuffer) != 0) {
...
}
}

void BlockOp::process() {
int i;
for (i = 0; i < iLimit; ++i) {
...
}
pthread_exit((void *) val++);
}
[...]

error: cannot convert `void (BlockOp::*)()' to `void*(*)(void*)' for
argument `3' to `int pthread_create(pthread_t*, const pthread_attr_t*,
void*(*)(void*), void*)' BlockOp.cpp blockop line 17

Any idea how to clear it up?

Regards,
T h o m a s B
ben
Guest
 
Posts: n/a
#2: Oct 9 '05

re: Trouble with pointer to a function


Thomas Barth wrote:[color=blue]
> Hi,
> I dont understand the third argument of this function. This function
> expect the address of a function, but what does "void *(*funktion)(void
> *)," mean? I am confused by so many "wildcards" :-)
>
> int pthread_create( pthread_t *thread,
> const pthread_attr_t *attribute,
> void *(*funktion)(void *),
> void *argumente );[/color]

A pointer to function can be declared as ReturnT (*PtrToFuncT)(ArgT1,
ArgT2...). So void* (*function)(void*) means "function" is a
pointer-to-a function which takes a pointer-to-void as an parameter and
returns a pointer-to-void.

So say you have a function called hello:

void* hello(void*);

Then you can assign function with hello:

void* (*function)(void*);
function = &hello;
[color=blue]
>
>
> When invoking the pthread_create function I get an error at compile time.
>
> [...]
> void BlockOp::start() {
> if(pthread_create(&pth, NULL, &BlockOp::process, cBuffer) != 0) {
> ...
> }
> }
>
> void BlockOp::process() {
> int i;
> for (i = 0; i < iLimit; ++i) {
> ...
> }
> pthread_exit((void *) val++);
> }
> [...]
>
> error: cannot convert `void (BlockOp::*)()' to `void*(*)(void*)' for
> argument `3' to `int pthread_create(pthread_t*, const pthread_attr_t*,
> void*(*)(void*), void*)' BlockOp.cpp blockop line 17
>[/color]


From the compiler error we know that BlockOp::process takes no argument
and returns nothing at all, assuming it is a static member function.

In otherwords, Block::process, assumed static, is of type void(void).
Note that pthread_create requires a pointer to void*(void*) and you
can't therefore pass Block::process.

[color=blue]
> Any idea how to clear it up?
>
> Regards,
> T h o m a s B[/color]

If you really don't care about the void* argument you can wrap up
Block::process like so:

void* thread_entry(void* trivial)
{
Block::process();
}

Ben

n2xssvv g02gfr12930
Guest
 
Posts: n/a
#3: Oct 9 '05

re: Trouble with pointer to a function


Thomas Barth wrote:[color=blue]
> Hi,
> I dont understand the third argument of this function. This function
> expect the address of a function, but what does "void *(*funktion)(void
> *)," mean? I am confused by so many "wildcards" :-)
>
> int pthread_create( pthread_t *thread,
> const pthread_attr_t *attribute,
> void *(*funktion)(void *),
> void *argumente );
>
>
> When invoking the pthread_create function I get an error at compile time.
>
> [...]
> void BlockOp::start() {
> if(pthread_create(&pth, NULL, &BlockOp::process, cBuffer) != 0) {
> ...
> }
> }
>
> void BlockOp::process() {
> int i;
> for (i = 0; i < iLimit; ++i) {
> ...
> }
> pthread_exit((void *) val++);
> }
> [...]
>
> error: cannot convert `void (BlockOp::*)()' to `void*(*)(void*)' for
> argument `3' to `int pthread_create(pthread_t*, const pthread_attr_t*,
> void*(*)(void*), void*)' BlockOp.cpp blockop line 17
>
> Any idea how to clear it up?
>
> Regards,
> T h o m a s B[/color]

The function required by pthread_create() is of the form

void *thread_fnc(void *Data)

You tried to pass a pointer to a non static member function
Create a static member function and pass a pointer to an array
of pointers for the object and required data that can then be
used to call the required member function. Hopefully the sample
code below will point you in the right direction.

class Example
{
public:
static void *thread_fnc(void *pData);

private:
void My_Fnc(char *pBuffer);
};

void *Example::thread_fnc(void *pData)
{
void **p = reinterpret_cast<void **>(pData);
Example *pObj = reinterpret_cast<Example *>(p[0])
char *pBuffer = reinterpret_cast<char *>(p[1])
pObj->My_Fnc(pBuffer);
}


void Some_fnc(void)
{
pthread_t Thr;
ben
Guest
 
Posts: n/a
#4: Oct 9 '05

re: Trouble with pointer to a function


Thomas Barth wrote:
[color=blue]
> Hi,
> I dont understand the third argument of this function. This function[/color]
expect the address of a function, but what does "void *(*funktion)(void
*)," mean? I am confused by so many "wildcards" :-)[color=blue]
>
> int pthread_create( pthread_t *thread,
> const pthread_attr_t *attribute,
> void *(*funktion)(void *),
> void *argumente );[/color]


A pointer to function can be declared as ReturnT (*PtrToFuncT)(ArgT1,
ArgT2...). So void* (*function)(void*) means "function" is a
pointer-to-a function which takes a pointer-to-void as an parameter and
returns a pointer-to-void.

So say you have a function called hello:

void* hello(void*);

Then you can assign function with hello:

void* (*function)(void*);
function = &hello;
[color=blue]
>
>
> When invoking the pthread_create function I get an error at compile time.
>
> [...]
> void BlockOp::start() {
> if(pthread_create(&pth, NULL, &BlockOp::process, cBuffer) != 0) {
> ...
> }
> }
>
> void BlockOp::process() {
> int i;
> for (i = 0; i < iLimit; ++i) {
> ...
> }
> pthread_exit((void *) val++);
> }
> [...]
>
> error: cannot convert `void (BlockOp::*)()' to `void*(*)(void*)' for[/color]
argument `3' to `int pthread_create(pthread_t*, const pthread_attr_t*,
void*(*)(void*), void*)' BlockOp.cpp blockop line 17[color=blue]
>[/color]


From the compiler error we know that BlockOp::process takes no argument
and returns nothing at all, assuming it is a static member function.

In otherwords, Block::process, assumed static, is of type void(void).
Note that pthread_create requires a pointer to void*(void*) and you
can't therefore pass Block::process.

[color=blue]
> Any idea how to clear it up?
>
> Regards,
> T h o m a s B[/color]


If you really don't care about the void* argument you can wrap up
Block::process like so:

void* thread_entry(void* trivial)
{
Block::process();
}

Ben


Closed Thread