In my code, I use QueueUserAPC
to interrupt the main thread from his current work in order to invoke some callback first before going back to his previous work.
std::string buffer;
std::tr1::shared_ptr<void> hMainThread;
VOID CALLBACK myCallback (ULONG_PTR dwParam) {
FILE * f = fopen("somefile", "a");
fprintf(f, "CALLBACK WAS INVOKED!
");
fclose(f);
}
void AdditionalThread () {
// download some file using synchronous wininet and store the
// HTTP response in buffer
QueueUserAPC(myCallback, hMainThread.get(), (ULONG_PTR)0);
}
void storeHandle () {
HANDLE hUnsafe;
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
GetCurrentProcess(), &hUnsafe, 0, FALSE, DUPLICATE_SAME_ACCESS);
hMainThread.reset(hUnsafe, CloseHandle);
}
void startSecondThread () {
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)AdditionalThread, 0, 0, NULL);
}
storeHandle
and startSecondThread
are exposed to a Lua interpreter which is running in the main thread along with other things. What I do now, is
- invoke
storeHandle
from my Lua interpreter.DuplicateHandle
returns a non-zero value and therefore succeeds. - invoke
startSecondThread
from my Lua interpreter. The additional thread gets started properly, andQueueUserAPC
returns a nonzero value, stating, that all went well. - as far as I understood
QueueUserAPC
,myCallback
should now get called from the main thread. However, it doesn t.
If QueueUserAPC
is the correct way to accomplish my goal (==> see my other question):
- How can I get this working?
If I should some other method to interrupt the main thread:
- What other method should I use? (Note that I don t want to use pull-ing method in the main thread for this like
WaitForSingleObject
or polling. I want that the additional thread push-es it s data straight into the main thread, as soon as possible.)