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

finish the hw05 #33

Open
wants to merge 1 commit 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
52 changes: 26 additions & 26 deletions MTQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,29 @@ class MTQueue {
}
};

int main() {
MTQueue<int> foods;

std::thread t1([&] {
for (int i = 0; i < 2; i++) {
auto food = foods.pop();
std::cout << "t1 got food:" << food << std::endl;
}
});

std::thread t2([&] {
for (int i = 0; i < 2; i++) {
auto food = foods.pop();
std::cout << "t2 got food:" << food << std::endl;
}
});

foods.push(42);
foods.push(233);
foods.push_many({666, 4399});

t1.join();
t2.join();

return 0;
}
//int main() {
// MTQueue<int> foods;
//
// std::thread t1([&] {
// for (int i = 0; i < 2; i++) {
// auto food = foods.pop();
// std::cout << "t1 got food:" << food << std::endl;
// }
// });
//
// std::thread t2([&] {
// for (int i = 0; i < 2; i++) {
// auto food = foods.pop();
// std::cout << "t2 got food:" << food << std::endl;
// }
// });
//
// foods.push(42);
// foods.push(233);
// foods.push_many({666, 4399});
//
// t1.join();
// t2.join();
//
// return 0;
//}
89 changes: 78 additions & 11 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
#include <string>
#include <thread>
#include <map>
#include <shared_mutex>
#include <mutex>

//reference:https://github.com/parallel101/hw05/pull/1/files

std::shared_mutex mtx_; // for username lock
std::shared_mutex mtx2_;// for has_login lock

struct User {
std::string password;
Expand All @@ -15,12 +21,14 @@ 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;

// 作业要求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_};
if (users.emplace(username, user).second)
return "注册成功";
else
Expand All @@ -29,13 +37,22 @@ 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) + "秒内登录过";
// long now = time(NULL); // C 语言当前时间
auto now = std::chrono::steady_clock::now();
{
std:: unique_lock ugrd{mtx2_};
if (has_login.find(username) != has_login.end()) {
// int sec = now - has_login.at(username); // C 语言算时间差
// return std::to_string(sec) + "秒内登录过";
auto dt = now - has_login.at(username);
if (dt <= std::chrono::seconds(10)) {
int64_t sec = std::chrono::duration_cast<std::chrono::seconds>(dt).count();
return std::to_string(sec) + "秒内登录过";
}
}
has_login[username] = now;
}
has_login[username] = now;

std::shared_lock grd{mtx_};
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) {
std::shared_lock grd{mtx_};
if( users.find(username) == users.end()){
return "没有该用户";
}
auto &user = users.at(username);
std::stringstream ss;
ss << "用户名: " << username << std::endl;
Expand All @@ -54,15 +75,58 @@ std::string do_queryuser(std::string username) {


struct ThreadPool {
std::vector<std::thread> workers;
std::vector<std::function<void()>> tasks;
std::mutex mtx;
std::condition_variable condition;
bool stop;

ThreadPool(int num_workers = std::thread::hardware_concurrency()):stop(false){
for(int i = 0;i < num_workers;++i){
workers.emplace_back([this]{
while(true){
std::function<void()> t;
{
std::unique_lock lck(mtx);
condition.wait(lck,[this]{return stop || !tasks.empty();});
if(stop && tasks.empty()) return ;
t = std::move(tasks.back());
tasks.pop_back();
}
t();
}
});
std::cout<<"Thread: "<<i<<" start"<<std::endl;
}
}

~ThreadPool(){
{
std::unique_lock lck{mtx};
stop = true;
condition.notify_all();
}
for(auto& worker:workers){
worker.join();
}
}


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

ThreadPool tpool;

//ThreadPool tpool;
ThreadPool tpool(12);

namespace test { // 测试用例?出水用力!
std::string username[] = {"张心欣", "王鑫磊", "彭于斌", "胡原名"};
Expand All @@ -72,7 +136,9 @@ std::string phone[] = {"110", "119", "120", "12315"};
}

int main() {
for (int i = 0; i < 262144; i++) {
for (int i = 0; i < 20000; i++) {
// 从test中不断随机采样
std::cout<<"for i #### = "<< i << std::endl;
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 +151,6 @@ int main() {
}

// 作业要求4:等待 tpool 中所有线程都结束后再退出
std::cout<<"all printed !" <<std::endl;
return 0;
}