Re: Casting function pointers




"KKL" <kishore.luthra@xxxxxxxxx> wrote in message
news:ec127657-7ed8-41ac-b951-cf974e627cf3@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

[example casting various member functions to task functions snipped]

1) You have to pass a compatible function pointer to the "TaskCreate"
function. Frequently this will be "void TaskType(void *arg)" or something
like "int TaskType(void *arg)". Some OSes might have additional parameters
but generally void * is enough. You generally can't cast a function pointer
to a different one, especially if the argument list is different, since the
stack will probably get misaligned or the arguemnts will get passed in the
wrong registers.

2) Member functions in C++ take the hidden "this" point argument so you can
never pass them to general "TaskCreate" type functions unless the OS knows
about the base class of your class.

What I usually do is this:

class ActiveClass
{
protected:
static void ThreadStarter(void *arg) { ((ActiveClass
*)arg)->ThreadProc(); }
void ThreadProc()
{
while (1)
{
DoTaskStuff();
}
}

public:
int Start() { TaskCreate(ThreadStarter, this); } // assumption: second
argument is what's passed to "arg"
};


The important thing is that ThreadStarter is static, which turns it into a
normal non-member function and that arg is cast to the type of the class
that has the member function with the task handling.

There are various ways of generalizing this approach with templates. You can
also pull out the ThreadStarter into a function outside of the class but I
like bundling things together as shown.

Andrew


.



Relevant Pages

  • Re: User defined stack for threads in Linux 2.6.11 + glibc 2.3.5
    ... printf, sleep(), and pthread_exitaren't async-cancel-safe, so the ... behavior is undefined if the thread running that code is actually ... The casting of 'fn' to void* is actually a constraint violation. ... structure that holds the function pointer and the value to pass as its ...
    (comp.programming.threads)
  • Re: returning function pointer
    ... function pointer some_func & meanwhile calls that function and prints ... void some_func ... int main ... Note there are two parameter lists here. ...
    (comp.lang.c)
  • Re: Common misconceptions about C (C95)
    ... I have never seen an example of function pointer ... and void pointer have any interaction. ... Great programmers make ... over the C programming world and only someone of very limited experience ...
    (comp.lang.c)
  • Re: Help a poor FORTRAN programmer with member functions?
    ... You need to use virtual member functions ... > the void* in the PrintObject function, though I thought I'd read that ... class Square: public Geometry{ ... void PrintObject(Geometry* aObjectPtr) ...
    (comp.lang.cpp)
  • Re: Passing a pointer to a function to the function it points to
    ... void* can't be used because it might not be able to represent a ... Any type of function pointer can portably losslessly point to any ... (Identical to is the easiest form of compatibility.) ...
    (comp.lang.c)