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

lambda 的参数是怎么传递的呀 #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
86 changes: 77 additions & 9 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
// 小彭老师作业05:假装是多线程 HTTP 服务器 - 富连网大厂面试官觉得很赞
// 小彭老师作业05:假装是多线程 HTTP 服务器 - 富连网大厂面试官觉得很赞
#include <functional>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <string>
#include <thread>
#include <map>
#include <shared_mutex>
#include <chrono>
#include <queue>


struct User {
std::string password;
std::string school;
std::string phone;
friend std::ostream& operator<< (std::ostream& stream, const User& user) {
stream << user.password + " " + user.school + " " + user.phone;
return stream;
}
};

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 mutex_users;
std::shared_mutex mutex_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};
// write
std::unique_lock lck(mutex_users);
if (users.emplace(username, user).second)
return "注册成功";
else
Expand All @@ -29,13 +41,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::system_clock::now().time_since_epoch());
{
// read and write
std::unique_lock lck(mutex_login);
if (has_login.find(username) != has_login.end()) {
int sec = (now - has_login.at(username)).count(); // C 语言算时间差
return std::to_string(sec) + "秒内登录过";
}
has_login[username] = now;
}
has_login[username] = now;

// read
std::shared_lock lck(mutex_users);
if (users.find(username) == users.end())
return "用户名错误";
if (users.at(username).password != password)
Expand All @@ -44,6 +61,10 @@ std::string do_login(std::string username, std::string password) {
}

std::string do_queryuser(std::string username) {
// read
std::shared_lock lck(mutex_users);
if (users.find(username) == users.end()) return "查询失败, 无效的用户名";

auto &user = users.at(username);
std::stringstream ss;
ss << "用户名: " << username << std::endl;
Expand All @@ -54,11 +75,58 @@ std::string do_queryuser(std::string username) {


struct ThreadPool {
using Job = std::function<void()>;
ThreadPool()
: m_stop_flag(false)
, m_poll_size(std::thread::hardware_concurrency())
{
for (size_t i = 0; i < m_poll_size; ++i) {
m_workers.emplace_back(
[&]() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可能改成[=]就可以了。

while (true)
{
Job job;
// 持续等待新任务进来
{
std::unique_lock lck(m_mutex);
m_condition.wait(lck, [&](){
return !m_jobs.empty() || m_stop_flag;
});
// 停止并且无任务
if (m_stop_flag && m_jobs.empty()) return;

job = m_jobs.front();
m_jobs.pop();
}
job();
}
}
);
}
}
void create(std::function<void()> start) {
// 作业要求3:如何让这个线程保持在后台执行不要退出?
// 提示:改成 async 和 future 且用法正确也可以加分
std::thread thr(start);
{
std::unique_lock lck(m_mutex);
m_jobs.push(start);
}
m_condition.notify_one();
}
~ThreadPool() {
m_stop_flag = true;
m_condition.notify_all();
for (auto& th : m_workers) {
th.join();
}
}
private:
bool m_stop_flag;
size_t m_poll_size;
std::condition_variable m_condition;
std::mutex m_mutex;
std::queue<Job> m_jobs;
std::vector<std::thread> m_workers;
};

ThreadPool tpool;
Expand Down