Communication lost in thread
My question is simple, I have made a server with Boost Asio.
It works well. The problem is when I launch std::thread, I cannot communicate anymore with the server.
Could you tell me why ? because I don't really get it.
Thanks !
here is my function who handle what I received :
void Server::handleCmd()
{
if (isClientExist() == false) {
addClient();
}
_vTmpString = Tools::splitString(_cmd, ' ');
_idCmd = Tools::stringToInt(_vTmpString[0]);
switch (_idCmd)
{
case CREATEROOM:
createRoom();
break;
case DISCONNECTION:
delClient();
break;
case GETROOM:
getVectorRoomsForClients();
break;
case JOINROOM:
joinRoom();
break;
case LEAVEROOM:
leaveRoom();
break;
case STARTROOM:
startGame();
break;
}
}
the function who launches the thread :
void Server::startGame()
{
_vTmpString = Tools::splitString(_cmd, ' ');
if (_vTmpString.size() != 2 && Tools::isNumber(_vTmpString[1]) == true)
return;
_tmpCmd = _vTmpString[0];
_tmpIdRoom = std::stoi(_vTmpString[1]);
if (_vTmpString.size() == 2 && _tmpCmd == "startroom" && _tmpIdRoom < 10000) {
sendMsg("OKn");
std::cout << "CLIENT " << _remoteEndPoint << " ASKS FOR STARTING THE ROOM " << CYAN << _tmpIdRoom << DEFAULT << " : " << GREEN << "OK" << DEFAULT << std::endl;
std::thread launchGame(&Server::gameLoop, this, _tmpIdRoom);
launchGame.join();
} else {
sendMsg("KOn");
std::cout << "CLIENT " << _remoteEndPoint << "ASKS FOR STARTING THE ROOM " << CYAN << _tmpIdRoom << DEFAULT << " : " << GREEN << "KO" << DEFAULT << std::endl;
}
}
and once I'm in the the thread, if i try to send any known commands, the server doesn't response anymore. the _cmd is blocked on the last one I sent before starting the server, it means "startroom".
c++ stdthread asio
add a comment |
My question is simple, I have made a server with Boost Asio.
It works well. The problem is when I launch std::thread, I cannot communicate anymore with the server.
Could you tell me why ? because I don't really get it.
Thanks !
here is my function who handle what I received :
void Server::handleCmd()
{
if (isClientExist() == false) {
addClient();
}
_vTmpString = Tools::splitString(_cmd, ' ');
_idCmd = Tools::stringToInt(_vTmpString[0]);
switch (_idCmd)
{
case CREATEROOM:
createRoom();
break;
case DISCONNECTION:
delClient();
break;
case GETROOM:
getVectorRoomsForClients();
break;
case JOINROOM:
joinRoom();
break;
case LEAVEROOM:
leaveRoom();
break;
case STARTROOM:
startGame();
break;
}
}
the function who launches the thread :
void Server::startGame()
{
_vTmpString = Tools::splitString(_cmd, ' ');
if (_vTmpString.size() != 2 && Tools::isNumber(_vTmpString[1]) == true)
return;
_tmpCmd = _vTmpString[0];
_tmpIdRoom = std::stoi(_vTmpString[1]);
if (_vTmpString.size() == 2 && _tmpCmd == "startroom" && _tmpIdRoom < 10000) {
sendMsg("OKn");
std::cout << "CLIENT " << _remoteEndPoint << " ASKS FOR STARTING THE ROOM " << CYAN << _tmpIdRoom << DEFAULT << " : " << GREEN << "OK" << DEFAULT << std::endl;
std::thread launchGame(&Server::gameLoop, this, _tmpIdRoom);
launchGame.join();
} else {
sendMsg("KOn");
std::cout << "CLIENT " << _remoteEndPoint << "ASKS FOR STARTING THE ROOM " << CYAN << _tmpIdRoom << DEFAULT << " : " << GREEN << "KO" << DEFAULT << std::endl;
}
}
and once I'm in the the thread, if i try to send any known commands, the server doesn't response anymore. the _cmd is blocked on the last one I sent before starting the server, it means "startroom".
c++ stdthread asio
1
There is not enough information (nor code) in this question to be able to answer. Please post a Minimal, Complete, and Verifiable example and be more explicit about what you expect and what you observe.
– Jesper Juhl
Dec 27 '18 at 15:09
add a comment |
My question is simple, I have made a server with Boost Asio.
It works well. The problem is when I launch std::thread, I cannot communicate anymore with the server.
Could you tell me why ? because I don't really get it.
Thanks !
here is my function who handle what I received :
void Server::handleCmd()
{
if (isClientExist() == false) {
addClient();
}
_vTmpString = Tools::splitString(_cmd, ' ');
_idCmd = Tools::stringToInt(_vTmpString[0]);
switch (_idCmd)
{
case CREATEROOM:
createRoom();
break;
case DISCONNECTION:
delClient();
break;
case GETROOM:
getVectorRoomsForClients();
break;
case JOINROOM:
joinRoom();
break;
case LEAVEROOM:
leaveRoom();
break;
case STARTROOM:
startGame();
break;
}
}
the function who launches the thread :
void Server::startGame()
{
_vTmpString = Tools::splitString(_cmd, ' ');
if (_vTmpString.size() != 2 && Tools::isNumber(_vTmpString[1]) == true)
return;
_tmpCmd = _vTmpString[0];
_tmpIdRoom = std::stoi(_vTmpString[1]);
if (_vTmpString.size() == 2 && _tmpCmd == "startroom" && _tmpIdRoom < 10000) {
sendMsg("OKn");
std::cout << "CLIENT " << _remoteEndPoint << " ASKS FOR STARTING THE ROOM " << CYAN << _tmpIdRoom << DEFAULT << " : " << GREEN << "OK" << DEFAULT << std::endl;
std::thread launchGame(&Server::gameLoop, this, _tmpIdRoom);
launchGame.join();
} else {
sendMsg("KOn");
std::cout << "CLIENT " << _remoteEndPoint << "ASKS FOR STARTING THE ROOM " << CYAN << _tmpIdRoom << DEFAULT << " : " << GREEN << "KO" << DEFAULT << std::endl;
}
}
and once I'm in the the thread, if i try to send any known commands, the server doesn't response anymore. the _cmd is blocked on the last one I sent before starting the server, it means "startroom".
c++ stdthread asio
My question is simple, I have made a server with Boost Asio.
It works well. The problem is when I launch std::thread, I cannot communicate anymore with the server.
Could you tell me why ? because I don't really get it.
Thanks !
here is my function who handle what I received :
void Server::handleCmd()
{
if (isClientExist() == false) {
addClient();
}
_vTmpString = Tools::splitString(_cmd, ' ');
_idCmd = Tools::stringToInt(_vTmpString[0]);
switch (_idCmd)
{
case CREATEROOM:
createRoom();
break;
case DISCONNECTION:
delClient();
break;
case GETROOM:
getVectorRoomsForClients();
break;
case JOINROOM:
joinRoom();
break;
case LEAVEROOM:
leaveRoom();
break;
case STARTROOM:
startGame();
break;
}
}
the function who launches the thread :
void Server::startGame()
{
_vTmpString = Tools::splitString(_cmd, ' ');
if (_vTmpString.size() != 2 && Tools::isNumber(_vTmpString[1]) == true)
return;
_tmpCmd = _vTmpString[0];
_tmpIdRoom = std::stoi(_vTmpString[1]);
if (_vTmpString.size() == 2 && _tmpCmd == "startroom" && _tmpIdRoom < 10000) {
sendMsg("OKn");
std::cout << "CLIENT " << _remoteEndPoint << " ASKS FOR STARTING THE ROOM " << CYAN << _tmpIdRoom << DEFAULT << " : " << GREEN << "OK" << DEFAULT << std::endl;
std::thread launchGame(&Server::gameLoop, this, _tmpIdRoom);
launchGame.join();
} else {
sendMsg("KOn");
std::cout << "CLIENT " << _remoteEndPoint << "ASKS FOR STARTING THE ROOM " << CYAN << _tmpIdRoom << DEFAULT << " : " << GREEN << "KO" << DEFAULT << std::endl;
}
}
and once I'm in the the thread, if i try to send any known commands, the server doesn't response anymore. the _cmd is blocked on the last one I sent before starting the server, it means "startroom".
c++ stdthread asio
c++ stdthread asio
edited Dec 27 '18 at 15:12
asked Dec 27 '18 at 15:05
jim2k
246
246
1
There is not enough information (nor code) in this question to be able to answer. Please post a Minimal, Complete, and Verifiable example and be more explicit about what you expect and what you observe.
– Jesper Juhl
Dec 27 '18 at 15:09
add a comment |
1
There is not enough information (nor code) in this question to be able to answer. Please post a Minimal, Complete, and Verifiable example and be more explicit about what you expect and what you observe.
– Jesper Juhl
Dec 27 '18 at 15:09
1
1
There is not enough information (nor code) in this question to be able to answer. Please post a Minimal, Complete, and Verifiable example and be more explicit about what you expect and what you observe.
– Jesper Juhl
Dec 27 '18 at 15:09
There is not enough information (nor code) in this question to be able to answer. Please post a Minimal, Complete, and Verifiable example and be more explicit about what you expect and what you observe.
– Jesper Juhl
Dec 27 '18 at 15:09
add a comment |
1 Answer
1
active
oldest
votes
launchGame.join()
blocks until the thread terminates. And since the thread apparently runs a game loop, it won't terminate for a while. It makes little sense to join with it at this point.
hello, what do you mean by "joint with it" ?
– jim2k
Dec 27 '18 at 16:33
I mean calljoin()
method. That method doesn't return until the thread terminates, sostartGame
is probably not a right place to call it. As written, it's more likestartGameAndWaitForItToFinish
– Igor Tandetnik
Dec 27 '18 at 23:37
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53947081%2fcommunication-lost-in-thread%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
launchGame.join()
blocks until the thread terminates. And since the thread apparently runs a game loop, it won't terminate for a while. It makes little sense to join with it at this point.
hello, what do you mean by "joint with it" ?
– jim2k
Dec 27 '18 at 16:33
I mean calljoin()
method. That method doesn't return until the thread terminates, sostartGame
is probably not a right place to call it. As written, it's more likestartGameAndWaitForItToFinish
– Igor Tandetnik
Dec 27 '18 at 23:37
add a comment |
launchGame.join()
blocks until the thread terminates. And since the thread apparently runs a game loop, it won't terminate for a while. It makes little sense to join with it at this point.
hello, what do you mean by "joint with it" ?
– jim2k
Dec 27 '18 at 16:33
I mean calljoin()
method. That method doesn't return until the thread terminates, sostartGame
is probably not a right place to call it. As written, it's more likestartGameAndWaitForItToFinish
– Igor Tandetnik
Dec 27 '18 at 23:37
add a comment |
launchGame.join()
blocks until the thread terminates. And since the thread apparently runs a game loop, it won't terminate for a while. It makes little sense to join with it at this point.
launchGame.join()
blocks until the thread terminates. And since the thread apparently runs a game loop, it won't terminate for a while. It makes little sense to join with it at this point.
answered Dec 27 '18 at 15:49
Igor Tandetnik
31.3k33254
31.3k33254
hello, what do you mean by "joint with it" ?
– jim2k
Dec 27 '18 at 16:33
I mean calljoin()
method. That method doesn't return until the thread terminates, sostartGame
is probably not a right place to call it. As written, it's more likestartGameAndWaitForItToFinish
– Igor Tandetnik
Dec 27 '18 at 23:37
add a comment |
hello, what do you mean by "joint with it" ?
– jim2k
Dec 27 '18 at 16:33
I mean calljoin()
method. That method doesn't return until the thread terminates, sostartGame
is probably not a right place to call it. As written, it's more likestartGameAndWaitForItToFinish
– Igor Tandetnik
Dec 27 '18 at 23:37
hello, what do you mean by "joint with it" ?
– jim2k
Dec 27 '18 at 16:33
hello, what do you mean by "joint with it" ?
– jim2k
Dec 27 '18 at 16:33
I mean call
join()
method. That method doesn't return until the thread terminates, so startGame
is probably not a right place to call it. As written, it's more like startGameAndWaitForItToFinish
– Igor Tandetnik
Dec 27 '18 at 23:37
I mean call
join()
method. That method doesn't return until the thread terminates, so startGame
is probably not a right place to call it. As written, it's more like startGameAndWaitForItToFinish
– Igor Tandetnik
Dec 27 '18 at 23:37
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53947081%2fcommunication-lost-in-thread%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
There is not enough information (nor code) in this question to be able to answer. Please post a Minimal, Complete, and Verifiable example and be more explicit about what you expect and what you observe.
– Jesper Juhl
Dec 27 '18 at 15:09