For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > February 2007 > pthreads compiler error when thread function is part of a class ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author pthreads compiler error when thread function is part of a class ?
nass

2007-02-16, 8:08 am

hello everyone,
this is my first attempt to use pthreads to separate the execution of
a function that copies a big array to another one, and then packes
some of the data of this array tightly to another small array that
must then write asynchronously to the disk. i guess this altogether
might stall the execution of the monitor functionality so it might me
nice to use threads here.

anyhow i want to make a thread out of one of my class functions:

the class is

class LogBase

{
...
void logUpdate(); //this runs and if condition is met a thread
is created and runs the AsyncWriteLine() function
void *AsynchWriteLine(void *pThrdStruct); //a fileIO function
that is to run as separate thread
}

and its implementation

void LogBase::logUpdate()
{
FileTime CurTime;
CurTime=GetTime(); //get the Time from epoch
if (CurTime>=Period+PrWriteTime)
{
memcpy(pThrdArg,SensSt,sizeof(ThreadPass
)); // struct
ThreadPass *pThrdArg;
//
is the struct of args for the thread
pthread_t thrdWrt;
pthread_create(&thrdWrt,NULL,AsynchWriteLine,(void
*)pThrdArg); //this line doesn't compile

PrWriteTime=CurTime;
}
}

void *LogBase::AsynchWriteLine(void *pThrdStruct)
{
ThreadPass * pThrdData = (struct ThreadPass *) pThrdStruct;
....
....
}


i compile as g++ -c -g -pthread logger.cpp -o logger.o

and get the following error
logger.cpp: In member function `void LogBase::logUpdate()':
logger.cpp:281: error: argument of type `void*(LogBase:: )(void*)'
does not
match `void*(*)(void*)'

any ideas how to tackle this?
thank you in advance
nass
now when i run

Rainer Weikusat

2007-02-16, 8:08 am

"nass" <athanasios.silis@gmail.com> writes:

[...]

> and get the following error
> logger.cpp: In member function `void LogBase::logUpdate()':
> logger.cpp:281: error: argument of type `void*(LogBase:: )(void*)'
> does not
> match `void*(*)(void*)'
>
> any ideas how to tackle this?


A pointer to a member function is not compatible with a pointer to a
non-member function (there is an invisible 'this' argument passed to
the former). A possible solution would be to define a non-member
function as thread entry point and to pass a pointer to the object
instance as argument to this function. It could then cast the void *
to the correct type and call the member function.
Binary

2007-02-16, 8:08 am

On 2=D4=C216=C8=D5, =CF=C2=CE=E77=CA=B112=B7=D6, "nass" <athanasios.si...@g=
mail.com> wrote:
> hello everyone,
> this is my first attempt to use pthreads to separate the execution of
> a function that copies a big array to another one, and then packes
> some of the data of this array tightly to another small array that
> must then write asynchronously to the disk. i guess this altogether
> might stall the execution of the monitor functionality so it might me
> nice to use threads here.
>
> anyhow i want to make a thread out of one of my class functions:
>
> the class is
>
> class LogBase
>
> {
> ...
> void logUpdate(); //this runs and if condition is met a thread
> is created and runs the AsyncWriteLine() function
> void *AsynchWriteLine(void *pThrdStruct); //a fileIO function
> that is to run as separate thread
> }
>
> and its implementation
>
> void LogBase::logUpdate()
> {
> FileTime CurTime;
> CurTime=3DGetTime(); //get the Time from epoch
> if (CurTime>=3DPeriod+PrWriteTime)
> {
> memcpy(pThrdArg,SensSt,sizeof(ThreadPass
)); // struct
> ThreadPass *pThrdArg;
> =

//
> is the struct of args for the thread
> pthread_t thrdWrt;
> pthread_create(&thrdWrt,NULL,AsynchWriteLine,(void
> *)pThrdArg); //this line doesn't compile
>
> PrWriteTime=3DCurTime;
> }
> }
>
> void *LogBase::AsynchWriteLine(void *pThrdStruct)
> {
> ThreadPass * pThrdData =3D (struct ThreadPass *) pThrdStruct;
> ....
> ....
> }
>
> i compile as g++ -c -g -pthread logger.cpp -o logger.o
>
> and get the following error
> logger.cpp: In member function `void LogBase::logUpdate()':
> logger.cpp:281: error: argument of type `void*(LogBase:: )(void*)'
> does not
> match `void*(*)(void*)'
>
> any ideas how to tackle this?


You may need this:
http://www.parashift.com/c++-faq-li...to-members.html
> thank you in advance
> nass
> now when i run



David Schwartz

2007-02-17, 7:04 pm

On Feb 16, 3:12 am, "nass" <athanasios.si...@gmail.com> wrote:

> logger.cpp: In member function `void LogBase::logUpdate()':
> logger.cpp:281: error: argument of type `void*(LogBase:: )(void*)'
> does not
> match `void*(*)(void*)'
>
> any ideas how to tackle this?


The pthread_create function requires a normal function with C linkage
as its parameter. The typical solution is to create a function with C
linkage that is a friend of the class. Pass it a pointer to the class
instance, which it casts back to a class pointer and it then calls a
'Start' or 'ThreadEnter' function on that instance.

DS

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com