Answer the question
In order to leave comments, you need to log in
How to create a new c++ thread?
Hello, please tell me if I can open a stream using a lambda expression for example like this
#include "MouseManager.h"
MouseManager mouseManager;
if (recoil.x != 0 && recoil.y != 0) {
CreateThread(nullptr, 0, [this]() {
mouseManager.mouseMove(recoil.x, recoil.y, 100);
return 0;
}
, nullptr, 0, nullptr);
}
Answer the question
In order to leave comments, you need to log in
In general, no.
CreateThread takes a function pointer. It needs the address of the code you want to execute. And this same code must follow certain calling conventions (stdcall). Lambda cannot always be brought to this. The compiler needs to put the captured variables somewhere and somehow transfer them to the lambda code. The lambdas themselves do not necessarily use the same calling conventions that CreateThread needs.
Sometimes, if your lambda is stateless (does not capture any variables), then some compilers (vs, for example) will be able to convert this lambda to a function pointer. But this is not your case, because you have variables captured - you also need to somehow use recoil and mouseManager from inside the lambda.
If you really want a lambda, then purely theoretically, your variables can be put into global variables. And then the lambda won't capture anything. But this is the wildest govnokod - do not do this.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question