Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hw05 #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

hw05 #28

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
42 changes: 35 additions & 7 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// 小彭老师作业05:假装是多线程 HTTP 服务器 - 富连网大厂面试官觉得很赞
#include <chrono>
#include <functional>
#include <iostream>
#include <mutex>
#include <shared_mutex>
#include <sstream>
#include <cstdlib>
#include <string>
Expand All @@ -15,12 +18,14 @@ struct User {
};

std::map<std::string, User> users;
std::map<std::string, long> has_login; // 换成 std::chrono::seconds 之类的
std::map<std::string, std::chrono::seconds> has_login; // 换成 std::chrono::seconds 之类的
std::shared_mutex mtx_users, mtx_login;

// 作业要求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_users);
if (users.emplace(username, user).second)
return "注册成功";
else
Expand All @@ -29,13 +34,18 @@ 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::duration_cast<std::chrono::seconds>
(std::chrono::steady_clock::now().time_since_epoch());
{
std::unique_lock grd(mtx_login);
if (has_login.find(username) != has_login.end()) {
auto sec = now - has_login.at(username); // C 语言算时间差
return std::to_string(sec.count()) + "秒内登录过";
}
has_login[username] = now;
}
has_login[username] = now;

std::shared_lock grd(mtx_users);
if (users.find(username) == users.end())
return "用户名错误";
if (users.at(username).password != password)
Expand All @@ -44,7 +54,13 @@ std::string do_login(std::string username, std::string password) {
}

std::string do_queryuser(std::string username) {
auto &user = users.at(username);
std::shared_lock grd(mtx_users);
auto found = users.find(username);
if (found == users.end())
return "用户名不存在\n";
auto &user = found->second;
grd.unlock();

std::stringstream ss;
ss << "用户名: " << username << std::endl;
ss << "学校:" << user.school << std::endl;
Expand All @@ -54,10 +70,21 @@ std::string do_queryuser(std::string username) {


struct ThreadPool {
std::vector<std::thread> pool;

void create(std::function<void()> start) {
// 作业要求3:如何让这个线程保持在后台执行不要退出?
// 提示:改成 async 和 future 且用法正确也可以加分

// A: 维护一个全局线程池,全局的tpool销毁时调用析构函数循环join()
std::thread thr(start);
pool.push_back(std::move(thr));
}

~ThreadPool() {
for(auto &thr: pool) {
thr.join();
}
}
};

Expand Down Expand Up @@ -85,5 +112,6 @@ int main() {
}

// 作业要求4:等待 tpool 中所有线程都结束后再退出
// 全局的tpool析构时自动join()
return 0;
}