登錄後,我的程序需要等待4秒鐘,然後在寫入分數前檢查玩家是否仍然登錄。我調用該函數作爲異步之一:具有參數的異步C++ WaitForSeconds函數
std::async (scoreAttendance, p); //p = pointer to player
這是我的函數:
void scoreAttendance(player *p)
{
time_t beginT = time(NULL);
time_t endT = time(NULL);
while(difftime(endT, beginT) < p->attendingTime){
endT = time(NULL);}
if (p->user != NULL){
sendScore(saveScore);}
}
但它沒有這樣做異步,它擋住了整個程序持續4秒。我做錯了什麼?
附加信息: 該程序運行在遊戲服務器上。如果連接的RFID偵聽器讀取新的RFID,它會通過TCP/IP向遊戲服務器通知此登錄信息。
//TCP Server handles the received data
void session::handle_read(const boost::system::error_code& error,
size_t bytes_transferred)
{
[...]
std::vector<char> response =
packet.process(data_, bytes_transferred, ip);
[...]
}
//This leads to the process function
void process(datapacket& gamedata)
{
std::string command = gamedata.getvalue("command");
PROCESS_COMMAND(u,"plug/rfid/read", loginStation);
[...]
}
//process calls this login function
void loginStation(datapacket& gamedata)
{
[...]
//Identifies the User by his RFID
[...]
std::async (scoreAttendance, p);
return;
}
目前,您的問題沒有給出足夠的信息來診斷問題。你可以在你調用std :: async的地方添加一些上下文嗎?以[mcve](http://stackoverflow.com/help/mcve)向我們展示問題將是理想的。 – PeterSW