std::string_view 的使用

This commit is contained in:
2026-06-29 09:40:55 +08:00
parent ccf7eecc29
commit c25e58ae17
40 changed files with 532 additions and 522 deletions
+20 -17
View File
@@ -3,25 +3,27 @@
#include <iostream>
#include "../system/export.h"
#include <string_view>
using namespace std::chrono;
void Detach_Thread_Manager::start_thread(const std::string& name, const std::function<void(std::atomic<bool>&)>& f) {
auto iter = thread_map.find(name);
void Detach_Thread_Manager::start_thread(std::string_view name, const std::function<void(std::atomic<bool>&)>& f) {
auto name_string = std::string(name);
auto iter = thread_map.find(name_string);
if (iter == thread_map.end())
{
auto t = new Detach_Thread(name, [f, name, this](std::atomic<bool>& running) {
auto t = new Detach_Thread(name_string, [f, name_string, this](std::atomic<bool>& running) {
{
std::ostringstream oss;
oss << "\t" + name + "线程[id:" << std::this_thread::get_id() << "] 启动!\n";
oss << "\t" + name_string + "线程[id:" << std::this_thread::get_id() << "] 启动!\n";
std::cout << oss.str() << std::flush;
}
f(running);
{
std::ostringstream oss;
oss << "" + name + "】 线程 ";
oss << "" + name_string + "】 线程 ";
auto end = std::chrono::high_resolution_clock::now();
auto duration = duration_cast<std::chrono::milliseconds>(end - exit_time); // 转换为毫秒
oss << "耗时: " + std::to_string(duration.count()) + " 毫秒" + " 退出! "
@@ -29,18 +31,19 @@ void Detach_Thread_Manager::start_thread(const std::string& name, const std::fun
std::cout << oss.str() << std::endl;
}
});
thread_map[name] = t;
thread_map[name_string] = t;
t->start();
} else
{
std::cerr << "Detach_Thread_Manager append_thread error! Thread " << name << " already exists." << std::endl;
}
}
void Detach_Thread_Manager::exit_thread(const std::string& name) {
void Detach_Thread_Manager::exit_thread(std::string_view name) {
auto name_string = std::string(name);
bool ok = false;
for (auto it = thread_map.begin(); it != thread_map.end(); it++)
{
if (it->first == name)
if (it->first == name_string)
{
auto t = it->second;
t->running.store(false, std::memory_order_release);
@@ -48,7 +51,7 @@ void Detach_Thread_Manager::exit_thread(const std::string& name) {
{
// 等待所有线程退出
}
std::cout << "\t" + name + "线程关闭!\n";
std::cout << "\t" + name_string + "线程关闭!\n";
delete it->second;
thread_map.erase(it);
ok = true;
@@ -59,10 +62,10 @@ void Detach_Thread_Manager::exit_thread(const std::string& name) {
{
}
}
Detach_Thread::Detach_Thread(const std::string &name, const std::function<void(std::atomic<bool> &running)> &func) {
this->name = name;
thread = std::thread([func, this, name]() {
Psc::set_current_thread_name(name);
Detach_Thread::Detach_Thread(std::string_view name, const std::function<void(std::atomic<bool> &running)> &func) {
this->name = std::string(name);
thread = std::thread([func, this, name_string = this->name]() {
Psc::set_current_thread_name(name_string);
func(running);
// 标志守护函数已经退出
this->exit.store(true, std::memory_order_release);
@@ -71,15 +74,15 @@ Detach_Thread::Detach_Thread(const std::string &name, const std::function<void(s
void Detach_Thread::start() {
thread.detach();
}
void Detach_Thread_Manager::test_and_start_thread(const std::string &name,
void Detach_Thread_Manager::test_and_start_thread(std::string_view name,
const std::function<void(std::atomic<bool> &running)> &func) {
if (thread_map.find(name) == thread_map.end())
if (thread_map.find(std::string(name)) == thread_map.end())
{
start_thread(name, func);
}
}
void Detach_Thread_Manager::test_and_stop_thread(const std::string &name) {
if (thread_map.find(name) != thread_map.end())
void Detach_Thread_Manager::test_and_stop_thread(std::string_view name) {
if (thread_map.find(std::string(name)) != thread_map.end())
{
exit_thread(name);
}