From fa47e33d897835ea5b27d0b69d3e5ada3119d7cf Mon Sep 17 00:00:00 2001 From: jkjk Date: Fri, 29 Mar 2024 00:00:40 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=A4=9A=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index f4ecab8..eb6e6fb 100644 --- a/main.cpp +++ b/main.cpp @@ -6,6 +6,8 @@ #include #include #include +#include +#include struct User { @@ -15,12 +17,16 @@ struct User { }; std::map users; -std::map has_login; // 换成 std::chrono::seconds 之类的 +std::map has_login; // 换成 std::chrono::seconds 之类的 + +// std::mutex mtx; +std::shared_mutex mtx; // 作业要求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 grd(mtx); if (users.emplace(username, user).second) return "注册成功"; else @@ -29,11 +35,14 @@ 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 语言当前时间 + using namespace std::chrono; + steady_clock::time_point now = steady_clock::now(); if (has_login.find(username) != has_login.end()) { - int sec = now - has_login.at(username); // C 语言算时间差 + // int sec = now - has_login.at(username); // C 语言算时间差 + int sec = duration_cast(now - has_login.at(username)).count(); return std::to_string(sec) + "秒内登录过"; } + std::unique_lock grd(mtx); has_login[username] = now; if (users.find(username) == users.end()) @@ -44,6 +53,9 @@ std::string do_login(std::string username, std::string password) { } std::string do_queryuser(std::string username) { + std::shared_lock grd(mtx); + if (users.find(username) == users.end()) + return "用户名错误"; auto &user = users.at(username); std::stringstream ss; ss << "用户名: " << username << std::endl; @@ -54,10 +66,12 @@ std::string do_queryuser(std::string username) { struct ThreadPool { + std::vector pool; void create(std::function start) { // 作业要求3:如何让这个线程保持在后台执行不要退出? // 提示:改成 async 和 future 且用法正确也可以加分 std::thread thr(start); + pool.push_back(std::move(thr)); } }; @@ -85,5 +99,10 @@ int main() { } // 作业要求4:等待 tpool 中所有线程都结束后再退出 + for(auto &it: tpool.pool) + { + // std::cout << "on joining" ; + it.join(); //join在哪儿就等待在哪儿 + } return 0; } From e0506d3389f0fb46f69e5a5e8ed7945714fccfc6 Mon Sep 17 00:00:00 2001 From: jkjk Date: Fri, 29 Mar 2024 00:04:05 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E6=95=B0=E9=81=BF=E5=85=8D=E7=BA=BF=E7=A8=8B=E8=80=97=E5=B0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index eb6e6fb..305cc70 100644 --- a/main.cpp +++ b/main.cpp @@ -86,7 +86,7 @@ std::string phone[] = {"110", "119", "120", "12315"}; } int main() { - for (int i = 0; i < 262144; i++) { + for (int i = 0; i < 10000; i++) { tpool.create([&] { std::cout << do_register(test::username[rand() % 4], test::password[rand() % 4], test::school[rand() % 4], test::phone[rand() % 4]) << std::endl; }); From 07ecf645db9fb110daaa6a50e702565a73a63189 Mon Sep 17 00:00:00 2001 From: jkjk Date: Fri, 29 Mar 2024 00:07:44 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E4=B8=8A=E9=94=81=E6=97=B6=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 305cc70..0937947 100644 --- a/main.cpp +++ b/main.cpp @@ -35,6 +35,7 @@ std::string do_register(std::string username, std::string password, std::string std::string do_login(std::string username, std::string password) { // 作业要求2:把这个登录计时器改成基于 chrono 的 + std::unique_lock grd(mtx); using namespace std::chrono; steady_clock::time_point now = steady_clock::now(); if (has_login.find(username) != has_login.end()) { @@ -42,7 +43,6 @@ std::string do_login(std::string username, std::string password) { int sec = duration_cast(now - has_login.at(username)).count(); return std::to_string(sec) + "秒内登录过"; } - std::unique_lock grd(mtx); has_login[username] = now; if (users.find(username) == users.end())