Wednesday, 21 August 2013

Use CreateThread with a lambda

Use CreateThread with a lambda

Just experimenting, but I was wondering if it's possible to make this code
work:
void main() {
int number = 5;
DWORD(*dontThreadOnMe)(PVOID) = [](PVOID data) {
int value = *(int*) data;
cout << value << endl;
cout << "This callback executed successsfully" << endl;
};
CreateThread(NULL, NULL, dontThreadOnMe, &number, NULL, NULL);
cin.get();
}
I have this nagging suspicion that because the standard signature for a
LPTHREAD_START_ROUTINE callback is DWORD WINAPI Callback(PVOID) I won't be
able to get this to compile without the added (but grammatically illegal)
WINAPI tag. Speaking of which, what exactly are the WINAPI and CALLBACK
(for say WndProc) attributes? I've never really understood why in certain
circumstances you could have multiple attributes on a function.

No comments:

Post a Comment