Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build
GNUmakefile
.vs
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
41 changes: 32 additions & 9 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// 小彭老师作业05:假装是多线程 HTTP 服务器 - 富连网大厂面试官觉得很赞
// 小彭老师作业05:假装是多线程 HTTP 服务器 - 富连网大厂面试官觉得很赞
#include <functional>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <string>
#include <thread>
#include <map>

#include <chrono>
#include <mutex>
#include <shared_mutex>
#include <future>
#include <vector>

struct User {
std::string password;
Expand All @@ -15,12 +19,15 @@ struct User {
};

std::map<std::string, User> users;
std::map<std::string, long> has_login; // 换成 std::chrono::seconds 之类的
std::shared_mutex mtx;
std::map<std::string, std::chrono::system_clock::time_point> 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
Expand All @@ -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<std::chrono::seconds>(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)
Expand All @@ -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;
Expand All @@ -54,10 +66,19 @@ std::string do_queryuser(std::string username) {


struct ThreadPool {
std::vector<std::future<void>> vf;

void create(std::function<void()> 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();
}
}
};

Expand Down Expand Up @@ -85,5 +106,7 @@ int main() {
}

// 作业要求4:等待 tpool 中所有线程都结束后再退出
tpool.wait();

return 0;
}