Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
login
,register
等函数变成多线程安全的 - 10 分login
的登录计时器改成基于 chrono 的 - 5 分shared_mutex
区分读和写 - 10 分lock_guard
系列符合 RAII 思想 - 5 分并通过
main()
函数中的基本测试。users
以及has_login
,分别用shared_mutex
进行互斥保护。用shared_mutex
是为了实现读写锁。并且为了提高性能,尽量最小化互斥区代码,在适当的时候unlock
释放锁std::chrono::steady_clock::now()
获取时间,并用std::chrono::duration
转换为秒数shared_mutex
,见第一点unique_lock
以及shared_lock
实现读写锁,其实用lock_guard
性能会更高ThreadPool
中定义std::vector<std::future<void>>
数据成员,每次create
调用时,调用async
将返回值push
到vector
中。future
对象调用wait
。do_queryuser
中,不能直接users.at(username)
,否则可能会报out_of_range异常。