diff --git a/main.cpp b/main.cpp index f4ecab8..e3ce82a 100644 --- a/main.cpp +++ b/main.cpp @@ -6,7 +6,10 @@ #include #include #include - +#include +#include +#include +#include struct User { std::string password; @@ -15,12 +18,14 @@ struct User { }; std::map users; -std::map has_login; // 换成 std::chrono::seconds 之类的 +std::map has_login; // 换成 std::chrono::seconds 之类的 +std::shared_mutex users_smtx, has_login_smtx; // 作业要求1:把这些函数变成多线程安全的 // 提示:能正确利用 shared_mutex 加分,用 lock_guard 系列加分 std::string do_register(std::string username, std::string password, std::string school, std::string phone) { User user = {password, school, phone}; + std::unique_lock ugrd(users_smtx); if (users.emplace(username, user).second) return "注册成功"; else @@ -29,21 +34,35 @@ std::string do_register(std::string username, std::string password, std::string std::string do_login(std::string username, std::string password) { // 作业要求2:把这个登录计时器改成基于 chrono 的 - long now = time(NULL); // C 语言当前时间 - if (has_login.find(username) != has_login.end()) { - int sec = now - has_login.at(username); // C 语言算时间差 - return std::to_string(sec) + "秒内登录过"; + { + auto now = std::chrono::steady_clock::now(); // 当前时间 + { + std::shared_lock sgrd(has_login_smtx); + if (has_login.find(username) != has_login.end()) { + auto dt = now - has_login.at(username); // 时间差 + int sec = std::chrono::duration_cast(dt).count(); + return std::to_string(sec) + "秒内登录过"; + } + } + { + std::unique_lock ugrd(has_login_smtx); + has_login[username] = now; + } + } + { + std::shared_lock sgrd(users_smtx); + if (users.find(username) == users.end()) + return "用户名错误"; + if (users.at(username).password != password) + return "密码错误"; } - has_login[username] = now; - - if (users.find(username) == users.end()) - return "用户名错误"; - if (users.at(username).password != password) - return "密码错误"; return "登录成功"; } std::string do_queryuser(std::string username) { + std::shared_lock sgrd(users_smtx); + if (users.find(username) == users.end()) + return "query:用户名错误"; auto &user = users.at(username); std::stringstream ss; ss << "用户名: " << username << std::endl; @@ -54,10 +73,29 @@ std::string do_queryuser(std::string username) { struct ThreadPool { + std::list pool; + void create(std::function start) { // 作业要求3:如何让这个线程保持在后台执行不要退出? // 提示:改成 async 和 future 且用法正确也可以加分 std::thread thr(start); + pool.push_back(std::move(thr)); + } + void wait_finish() { + auto pt = pool.begin(); + while (!pool.empty()) { + if (pt->joinable()) { + pt->join(); + pt = pool.erase(pt); + } + else { + pt++; + if (pt==pool.end()) pt = pool.begin(); + } + } + } + ~ThreadPool() { + wait_finish(); } };