登录后,我的程序需要等待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