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的作业 #32

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
50 changes: 38 additions & 12 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <string>
#include <thread>
#include <map>
#include <mutex>
#include <shared_mutex>
#include <chrono>


struct User {
Expand All @@ -15,26 +18,41 @@ struct User {
};

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

std::mutex users_write_mutex;
std::shared_mutex users_read_mutex;
std::shared_mutex has_login_read_mutex;
std::mutex has_login_write_mutex;

// 作业要求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)
std::shared_lock grd_r(users_read_mutex);
if (users.find(username) == users.end()) {
std::unique_lock grd_w(users_write_mutex);
users.emplace(username, user);
return "注册成功";
else
}
else {
return "用户名已被注册";
}
}

std::string do_login(std::string username, std::string password) {
// 作业要求2:把这个登录计时器改成基于 chrono 的
long now = time(NULL); // C 语言当前时间
std::shared_lock grd_r(has_login_read_mutex);
auto t0 = std::chrono::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 语言算时间差
auto dt = t0 - has_login.at(username);
int sec = std::chrono::duration_cast<std::chrono::seconds>(dt).count();
return std::to_string(sec) + "秒内登录过";
}
has_login[username] = now;
std::unique_lock grd_w(has_login_write_mutex);
has_login[username] = t0;
grd_w.unlock();

if (users.find(username) == users.end())
return "用户名错误";
Expand All @@ -44,6 +62,7 @@ std::string do_login(std::string username, std::string password) {
}

std::string do_queryuser(std::string username) {
if(users.find(username) == users.end()) return "没有这样的用户";
auto &user = users.at(username);
std::stringstream ss;
ss << "用户名: " << username << std::endl;
Expand All @@ -52,13 +71,19 @@ std::string do_queryuser(std::string username) {
return ss.str();
}


struct ThreadPool {
class ThreadPool {
public:
void create(std::function<void()> start) {
// 作业要求3:如何让这个线程保持在后台执行不要退出?
// 提示:改成 async 和 future 且用法正确也可以加分
std::thread thr(start);
pool.push_back(std::move(thr));
}
~ThreadPool() {
for(auto& t : pool) {
t.join();
}
}
private:
std::vector<std::thread> pool;
};

ThreadPool tpool;
Expand All @@ -72,7 +97,7 @@ std::string phone[] = {"110", "119", "120", "12315"};
}

int main() {
for (int i = 0; i < 262144; i++) {
for (int i = 0; i < 1e4; i++) { // 262144
tpool.create([&] {
std::cout << do_register(test::username[rand() % 4], test::password[rand() % 4], test::school[rand() % 4], test::phone[rand() % 4]) << std::endl;
});
Expand All @@ -85,5 +110,6 @@ int main() {
}

// 作业要求4:等待 tpool 中所有线程都结束后再退出
// 在ThreadPool的析构函数中等待线程结束
return 0;
}
Loading