diff --git a/main.cpp b/main.cpp index f4ecab8..e92ea65 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,4 @@ -// 小彭老师作业05:假装是多线程 HTTP 服务器 - 富连网大厂面试官觉得很赞 +// Сʦҵ05װǶ߳ HTTP - Թپú #include #include #include @@ -6,6 +6,7 @@ #include #include #include +#include struct User { @@ -15,59 +16,87 @@ struct User { }; std::map users; -std::map has_login; // 换成 std::chrono::seconds 之类的 +std::map has_login; // std::chrono::seconds ֮ +std::shared_mutex m_usertMtx; +std::shared_mutex m_loginMtx; -// 作业要求1:把这些函数变成多线程安全的 -// 提示:能正确利用 shared_mutex 加分,用 lock_guard 系列加分 +// ҵҪ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}; - if (users.emplace(username, user).second) - return "注册成功"; + std::unique_lock grd(m_usertMtx); + bool ret = users.emplace(username, user).second; + if (ret) + return "עɹ"; else - return "用户名已被注册"; + return "ûѱע"; } 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) + "秒内登录过"; + // ҵҪ2¼ʱijɻ chrono + auto now = std::chrono::steady_clock::now(); + { + std::shared_lock grd(m_loginMtx); + if (has_login.find(username) != has_login.end()) { + auto diff = now - has_login.at(username); + int sec = std::chrono::duration_cast(diff).count(); + return std::to_string(sec) + "ڵ¼"; + } + } + { + std::unique_lock grd(m_loginMtx); + has_login[username] = now; } - has_login[username] = now; + std::shared_lock sl(m_usertMtx); if (users.find(username) == users.end()) - return "用户名错误"; + return "û"; if (users.at(username).password != password) - return "密码错误"; - return "登录成功"; + return ""; + return "¼ɹ"; } std::string do_queryuser(std::string username) { - auto &user = users.at(username); + std::shared_lock grd(m_usertMtx); + if (users.find(username) == users.end()) + return "û"; + + auto& user = users.at(username); std::stringstream ss; - ss << "用户名: " << username << std::endl; - ss << "学校:" << user.school << std::endl; - ss << "电话: " << user.phone << std::endl; + ss << "û: " << username << std::endl; + ss << "ѧУ:" << user.school << std::endl; + ss << "绰: " << user.phone << std::endl; return ss.str(); } struct ThreadPool { void create(std::function start) { - // 作业要求3:如何让这个线程保持在后台执行不要退出? - // 提示:改成 async 和 future 且用法正确也可以加分 + // ҵҪ3̱߳ںִ̨вҪ˳ + // ʾij async future ÷ȷҲԼӷ std::thread thr(start); + m_pool.push_back(std::move(thr)); } + + ~ThreadPool() + { + for (auto & thr : m_pool) + { + thr.join(); + } + } + +private: + std::vector m_pool; }; ThreadPool tpool; -namespace test { // 测试用例?出水用力! -std::string username[] = {"张心欣", "王鑫磊", "彭于斌", "胡原名"}; +namespace test { // ˮ +std::string username[] = {"", "", "ڱ", "ԭ"}; std::string password[] = {"hellojob", "anti-job42", "cihou233", "reCihou_!"}; -std::string school[] = {"九百八十五大鞋", "浙江大鞋", "剑桥大鞋", "麻绳理工鞋院"}; +std::string school[] = {"ŰٰʮЬ", "㽭Ь", "ŴЬ", "ЬԺ"}; std::string phone[] = {"110", "119", "120", "12315"}; } @@ -84,6 +113,6 @@ int main() { }); } - // 作业要求4:等待 tpool 中所有线程都结束后再退出 + // ҵҪ4ȴ tpool ̶߳˳ return 0; }