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 completed #43

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

hw05 completed #43

wants to merge 2 commits into from

Conversation

delve-wang
Copy link

  • login, register 等函数变成多线程安全的 - 10 分
  • login 的登录计时器改成基于 chrono 的 - 5 分
  • 能利用 shared_mutex 区分读和写 - 10 分
  • lock_guard 系列符合 RAII 思想 - 5 分
  • 让 ThreadPool::create 创建的线程保持后台运行不要退出 - 15 分
  • 等待 tpool 中所有线程都结束后再退出 - 5 分

并通过 main() 函数中的基本测试。

  1. 对全局变量users以及has_login,分别用shared_mutex进行互斥保护。用shared_mutex是为了实现读写锁。并且为了提高性能,尽量最小化互斥区代码,在适当的时候unlock释放锁
  2. 使用std::chrono::steady_clock::now()获取时间,并用std::chrono::duration转换为秒数
  3. 读写锁shared_mutex,见第一点
  4. unique_lock以及shared_lock实现读写锁,其实用lock_guard性能会更高
  5. ThreadPool中定义std::vector<std::future<void>>数据成员,每次create调用时,调用async将返回值pushvector中。
  6. 在main函数返回前,对线程池的每个future对象调用wait
  7. 另外,在do_queryuser中,不能直接users.at(username),否则可能会报out_of_range异常。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants