From 8352eac31ddaa19bba445935f3b34aeefb3ac70e Mon Sep 17 00:00:00 2001 From: luozhiya Date: Tue, 22 Mar 2022 19:24:21 +0800 Subject: [PATCH] abort ? --- .gitignore | 1 + CMakeLists.txt | 2 +- main.cpp | 41 ++++++++++++++++++++++++++++++++--------- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 9ed92a0..8c5aaa8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build GNUmakefile +.vs diff --git a/CMakeLists.txt b/CMakeLists.txt index c555708..586992a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.12) project(hellocmake LANGUAGES CXX) -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) if (NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) endif() diff --git a/main.cpp b/main.cpp index f4ecab8..fd70d63 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,4 @@ -// 小彭老师作业05:假装是多线程 HTTP 服务器 - 富连网大厂面试官觉得很赞 +// 小彭老师作业05:假装是多线程 HTTP 服务器 - 富连网大厂面试官觉得很赞 #include #include #include @@ -6,7 +6,11 @@ #include #include #include - +#include +#include +#include +#include +#include struct User { std::string password; @@ -15,12 +19,15 @@ struct User { }; std::map users; -std::map has_login; // 换成 std::chrono::seconds 之类的 +std::shared_mutex mtx; +std::map has_login; // 换成 std::chrono::seconds 之类的 +std::shared_mutex mtx_hl; // 作业要求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 lck(mtx); if (users.emplace(username, user).second) return "注册成功"; else @@ -29,13 +36,17 @@ 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) + "秒内登录过"; + { + std::unique_lock lck(mtx_hl); + auto now = std::chrono::system_clock::now(); // C 语言当前时间 + if (has_login.find(username) != has_login.end()) { + auto sec = std::chrono::duration_cast(now - has_login.at(username)).count(); // C 语言算时间差 + return std::to_string(sec) + "秒内登录过"; + } + has_login[username] = now; } - has_login[username] = now; + std::shared_lock lck(mtx); if (users.find(username) == users.end()) return "用户名错误"; if (users.at(username).password != password) @@ -44,6 +55,7 @@ std::string do_login(std::string username, std::string password) { } std::string do_queryuser(std::string username) { + std::shared_lock lck(mtx); auto &user = users.at(username); std::stringstream ss; ss << "用户名: " << username << std::endl; @@ -54,10 +66,19 @@ std::string do_queryuser(std::string username) { struct ThreadPool { + std::vector> vf; + void create(std::function start) { // 作业要求3:如何让这个线程保持在后台执行不要退出? // 提示:改成 async 和 future 且用法正确也可以加分 - std::thread thr(start); + // std::thread thr(start); + auto fret = std::async(start); + vf.push_back(std::move(fret)); + } + void wait () { + for (auto& f : vf) { + f.wait(); + } } }; @@ -85,5 +106,7 @@ int main() { } // 作业要求4:等待 tpool 中所有线程都结束后再退出 + tpool.wait(); + return 0; }