Malcolm
Malcolm

Reputation:

What does this mean?

This:

typedef HRESULT (*PFN_HANDLE)(ClassName&);

It's used like this:

DWORD ClassName::Wait(PFN_HANDLE pfnh_foo)
{
  while (!done) {
    waitCode = WaitForMultipleObjects(paramA, paramB, paramC, paramD)

    if (waitCode == WAIT_OBJECT_0)
    {
      pfnh_foo(*this);
    }
    else
      done;
  }
  return waitCode;
}

It appears that Wait does nothing except block when it gets to WaitForMultipleObjects and then after that does this strange pfnh_foo thing and either loops back around to wait again or exits

Upvotes: 0

Views: 392

Answers (8)

Eric
Eric

Reputation: 6436

It's defining a function pointer to a function with the prototype:

HRESULT someFunction(ClassName &)

It's then taking the function that's passed in and calling it using the current object as the parameter.

Upvotes: 2

user124493
user124493

Reputation:

This is a mutual exlcusion mechanism, or a means to cooperate between competing for resources... thus the need to "wait" for the objects.

edit: Wikipedia has a good intro on mutual exclusion and touches on some foundational issues, algorithms, and data structures. If you are new to mutual exclusion principles, it's worth a read.

http://en.wikipedia.org/wiki/Mutual_exclusion

Upvotes: 2

Malcolm
Malcolm

Reputation:

So i could just remove the function pointer and call the function directly at that point. It would only make sense to use a function pointer if there were more than one function that would need to handle whatever object is signled when the WaitforMulitpleObject returns in Wait().

ie.

HRESULT Foo(ClassName& myClass);
HRESULT Bar(ClassName& myClass);

anotherFunction(...)
{
  Wait(Foo);
  Wiat(Bar);
}

Upvotes: 0

aJ.
aJ.

Reputation: 35490

Your wait() function basically waits for multiple objects and then invoke function using function pointer PFN_HANDLE if the wait is successful ( indicated by return value WAIT_OBJECT_0).

pfnh_foo(*this);

This calls the function pointed by pfnh_foo with argument *this.

Lets say we have function:

HRESULT someFunction(ClassName& myClassInstance)
{
//blah .. blah
}

Wait will be invoked like this:

PFN_HANDLE pfnh_foo = &someFunction;  //function pointer to someFunction
wait(pfnh_foo);

Upvotes: 7

zebrabox
zebrabox

Reputation: 5774

This is thread sync code.
Looks to me like ClassName:Wait is running in a separate thread and waiting for one of the specified objects to signal that it's free before calling the callback
It's being used to avoid a race condition

Upvotes: 2

Tadeusz A. Kadłubowski
Tadeusz A. Kadłubowski

Reputation: 8363

pfnh_foo is a function pointer. You can use functions like normal variables.

typedef HRESULT (*PFN_HANDLE)(ClassName&) means that PFN_HANDLE is a pointer to a function of signature:

HRESULT foo(ClassName&)

Upvotes: 2

Otávio Décio
Otávio Décio

Reputation: 74290

WAIT_OBJECT_0 is the first handle you're waiting for, if it is it executes pfnh_foo, for any other wait handle it exits.

Upvotes: 2

Ana Betts
Ana Betts

Reputation: 74692

pfnh_foo is a function pointer - it's running a function, with your class as a parameter

Upvotes: 2

Related Questions