Greetings,
I think I’ve hit my limit of my C++ understanding. If anyone here can help, it would be very much appreciated.
I have a function in my program that takes user input and uses it to authenticate. This function first creates the default client and then starts a thread for the tick. Then it calls Nakama’s _client→AuthenticateEmail()
If authentication is successful all is well. But if authentication is unsuccessful I need to Join the ticker thread inside the Error Callback. If I don’t do this, then when the user tries to auth again and we reinitialize the client, we wind up initializing a thread that’s already running, which causes a crash.
I can join the thread ordinarily with theTicker.join(). This works in my class destructor and in my signout function. But when I execute theTicker.join() inside the Error Callback Lambda I get _RESOURCE_DEADLOCK_WOULD_OCCUR error.
Below is a paraphrased snippet of my code. If I’m completely missing the point, please do let me know. Thank you!
class NakamaSessionManager
{
private:
NClientPtr _client;
NSessionPtr _session;
NRtClientPtr _rtClient;
std::thread _theTicker;
bool _clientActive = false;
void initializeClient() {
NClientParameters parameters;
_client = createDefaultClient(parameters);
_clientActive = true;
_theTicker = std::thread{ &NakamaSessionManager::theTick, this };
return;
}
void theTick() {
while (_clientActive) {
_client->tick();
if (_rtClient) {
_rtClient->tick();
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
}
void authenticateClientEmail(…) {
auto successCallback = [](NSessionPtr session)
{ ... };
auto errorCallback = [this](const NError& error)
{
_clientActive = false;
if (_theTicker.joinable()) {
_theTicker.join(); //THIS LINE GIVES ERROR: _RESOURCE_DEADLOCK_WOULD_OCCUR.
};
_client->authenticateEmail(...);
return;
}
public:
void signInEmail(…){
initializeClient();
authenticateClientEmail(…);
return;
}
}