roybj
roybj

Reputation: 278

winapi apc function parameter passing - what is the best practice

Hi i using winapi's QueueUserAPC to invoke an apc function call in another thread. my question is, what is the best practice for passing a parameter to it. i refer to the object lifetime and allocation/deallocation responsibility.

DWORD WINAPI QueueUserAPC(PAPCFUNC pfnAPC, HANDLE hThread, ULONG_PTR dwData);

i am using the dwData to pass the parameter to pass a pointer to some data and i was wondering how i should handle it. i need to make sure that it lives until the receiving thread finished using it. should i use a smart pointer to make sure that data is deallocated when no longer used?

i guess that allocation in the calling thread and dealloc. in the receiving is possible but probably not such a good thing.

anything else that can be done? i think i would like to avoid synchronization between the two only to notify that the receiving thread is done with the data...

thanks!

Upvotes: 0

Views: 190

Answers (1)

rodrigo
rodrigo

Reputation: 98358

Alloc'ing in the sending thread and dealloc'ing in the receiving one is easy, but it has the main drawback that it may leak, even if you handle the sending failure, the receiving thread may finish before having a chance to execute the APC.

Probably your easiest way to avoid the leak is to create a queue for sent data -maybe a queue per thread- and when thread finishes, you traverse the thread queue and free all the pending data.

But as usual, the devil is in the details...

Upvotes: 1

Related Questions