From e31b5d3db5bfd835435ddec5f1af95c673f5d377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=AE=E7=94=9F=E8=8B=A5=E6=A2=A6?= <1070753498@qq.com> Date: Mon, 15 Apr 2024 19:35:43 +0800 Subject: [PATCH] crashpad; --- .github/workflows/build.yml | 8 +-- AtomicQueue/atomicqueue_unittest.cc | 3 + Breakpad/breakpad.hpp | 6 +- Breakpad/main.cc | 10 +++- CMakeLists.txt | 78 +++++++++++++++----------- CountDownLatch/countdownlatch.hpp | 2 +- Crashpad/CMakeLists.txt | 13 +++++ Crashpad/crashpad.cc | 59 +++++++++++++++++++ Crashpad/crashpad.hpp | 23 ++++++++ Crashpad/main.cc | 23 ++++++++ Curl/httpclient.hpp | 2 +- Curl/httpclient_async.hpp | 4 +- Curl/tcpclient.cc | 2 +- Curl/tcpclient.hpp | 2 +- DesignPattern/Singleton/singleton.hpp | 2 +- Glog/main.cc | 6 +- Icmp/icmp.hpp | 2 +- MonitorDir/CMakeLists.txt | 2 +- MonitorDir/monitordir.hpp | 2 +- Mutex/mutex.hpp | 2 +- OpenSSL/openssl_rsa.cc | 2 +- README.md | 38 +++++++------ Thread/thread.hpp | 10 ++-- object.hpp => utils/object.hpp | 0 scopeguard.hpp => utils/scopeguard.hpp | 0 vcpkg.json | 6 +- 26 files changed, 227 insertions(+), 80 deletions(-) create mode 100644 Crashpad/CMakeLists.txt create mode 100644 Crashpad/crashpad.cc create mode 100644 Crashpad/crashpad.hpp create mode 100644 Crashpad/main.cc rename object.hpp => utils/object.hpp (100%) rename scopeguard.hpp => utils/scopeguard.hpp (100%) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 44afcbf..3e861c3 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -50,7 +50,7 @@ jobs: if: startsWith(matrix.os, 'macos') shell: bash run: | - brew install ninja + brew install ninja python-setuptools ninja --version cmake --version clang --version @@ -58,7 +58,7 @@ jobs: if: startsWith(matrix.os, 'ubuntu') run: | sudo apt-get update - sudo apt-get install ninja-build + sudo apt-get install ninja-build clang libcurl4-openssl-dev ninja --version cmake --version gcc --version @@ -99,7 +99,7 @@ jobs: -DCMAKE_CXX_COMPILER=cl \ -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \ -G "${{ matrix.generators }}" \ - -DCMAKE_INSTALL_PREFIX:PATH=instdir + || (cat ./build/vcpkg_installed/vcpkg/issue_body.md && exit 1) - name: Configure macos or ubuntu if: startsWith(matrix.os, 'macos') || startsWith(matrix.os, 'ubuntu') shell: bash @@ -109,7 +109,7 @@ jobs: -B ./build \ -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \ -G "${{ matrix.generators }}" \ - -DCMAKE_INSTALL_PREFIX:PATH=instdir + || (cat ./build/vcpkg_installed/vcpkg/issue_body.md && exit 1) - name: Build shell: bash run: | diff --git a/AtomicQueue/atomicqueue_unittest.cc b/AtomicQueue/atomicqueue_unittest.cc index f470d55..5b975d6 100644 --- a/AtomicQueue/atomicqueue_unittest.cc +++ b/AtomicQueue/atomicqueue_unittest.cc @@ -44,6 +44,7 @@ TEST(AtomicQueue, PushPopManyThreads) { AtomicQueue queue; std::vector threads; + threads.reserve(100); for (int i = 0; i < 100; ++i) { threads.emplace_back([&queue, i] { queue.push(i); }); } @@ -62,6 +63,7 @@ TEST(AtomicQueue, PushPopManyThreads2) { AtomicQueue queue; std::vector threads; + threads.reserve(100); for (int i = 0; i < 100; ++i) { threads.emplace_back([&queue, i] { queue.push(i); @@ -79,6 +81,7 @@ TEST(AtomicQueue, PushPopManyThreads3) { AtomicQueue queue; std::vector threads; + threads.reserve(100); for (int i = 0; i < 100; ++i) { threads.emplace_back([&queue, i] { queue.push(i); diff --git a/Breakpad/breakpad.hpp b/Breakpad/breakpad.hpp index e8b9f7c..be45dc0 100644 --- a/Breakpad/breakpad.hpp +++ b/Breakpad/breakpad.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -7,10 +9,10 @@ namespace google_breakpad { class ExceptionHandler; } -class Breakpad +class Breakpad : noncopyable { public: - Breakpad(const std::string &dump_path); + explicit Breakpad(const std::string &dump_path); ~Breakpad(); private: diff --git a/Breakpad/main.cc b/Breakpad/main.cc index 1f672c7..b88ab45 100644 --- a/Breakpad/main.cc +++ b/Breakpad/main.cc @@ -1,10 +1,16 @@ #include "breakpad.hpp" -auto main(int argc, char *argv[]) -> int +void crash() { - Breakpad breakpad("./"); int *p = nullptr; *p = 1; +} + +auto main(int argc, char *argv[]) -> int +{ + Breakpad breakpad("./"); + + crash(); return 0; } diff --git a/CMakeLists.txt b/CMakeLists.txt index 23da6d3..f370a14 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,4 @@ -# 设定版本号 -cmake_minimum_required(VERSION 3.18) +cmake_minimum_required(VERSION 3.25.1) if(CMAKE_HOST_WIN32) set(CMAKE_TOOLCHAIN_FILE @@ -9,7 +8,7 @@ elseif(CMAKE_HOST_APPLE) set(CMAKE_TOOLCHAIN_FILE "/usr/local/share/vcpkg/scripts/buildsystems/vcpkg.cmake" CACHE STRING "Vcpkg toolchain file") -elseif(CMAKE_HOST_UNIX) +elseif(CMAKE_HOST_LINUX) set(CMAKE_TOOLCHAIN_FILE "/usr/local/share/vcpkg/scripts/buildsystems/vcpkg.cmake" CACHE STRING "Vcpkg toolchain file") @@ -18,19 +17,16 @@ endif() # 设定工程名 project( Cpp-Examples - VERSION 0.1 + VERSION 0.0.1 + DESCRIPTION "This is Cpp-Examples" + HOMEPAGE_URL "https://github.com/RealChuan/Cpp-Examples" LANGUAGES C CXX) -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) - -set(CMAKE_INCLUDE_CURRENT_DIR ON) -set(CMAKE_CURRENT_SOURCE_DIR ON) - -# set(CMAKE_CXX_FLAGS_DEBUG "-O0") set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG") - set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_DEBUG_POSTFIX d) if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) @@ -44,32 +40,20 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) endif() if(CMAKE_SIZEOF_VOID_P EQUAL 8) - set(CURRENT_PLATFORM "-64") + set(BITS "64") else() - set(CURRENT_PLATFORM "-32") + set(BITS "32") endif() -message(STATUS "Current Platform is ${CURRENT_PLATFORM}") +message(STATUS "Current Platform is ${BITS} bits.") -# 设定可执行二进制文件的目录 set(EXECUTABLE_OUTPUT_PATH - ${PROJECT_SOURCE_DIR}/bin${CURRENT_PLATFORM}/${CMAKE_BUILD_TYPE}) # 源文件目录 -# 设定存放编译出来的库文件的目录 -set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin${CURRENT_PLATFORM}/libs) -# 并且把该目录设为连接目录 + ${PROJECT_SOURCE_DIR}/bin-${BITS}/${CMAKE_BUILD_TYPE}) +set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin-${BITS}/libs) link_directories(${LIBRARY_OUTPUT_PATH}) include_directories(${PROJECT_SOURCE_DIR}) -message("CMAKE_MAJOR_VERSION: ${CMAKE_MAJOR_VERSION}") -message("CMAKE_MINOR_VERSION: ${CMAKE_MINOR_VERSION}") -message("CMAKE_PATCH_VERSION: ${CMAKE_PATCH_VERSION}") -message("CMAKE_TWEAK_VERSION: ${CMAKE_TWEAK_VERSION}") -message("CMAKE_VERSION: ${CMAKE_VERSION}") -message("CMAKE_GENERATOR: ${CMAKE_GENERATOR}") -message("CMAKE_C_COMPILER_ID: ${CMAKE_C_COMPILER_ID}") -message("CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}") - find_package(unofficial-breakpad CONFIG REQUIRED) if(unofficial-breakpad_FOUND) message(STATUS "found unofficial-breakpad") @@ -94,7 +78,13 @@ find_package(CURL CONFIG REQUIRED) if(CURL_FOUND) message(STATUS "found CURL") endif() +find_package(crashpad) +# vcpkg install crashpad, it is not good for macos and linux +if(crashpad_FOUND) + message(STATUS "found crashpad") +endif() +include(CTest) enable_testing() add_subdirectory(Algorithm) @@ -103,6 +93,9 @@ add_subdirectory(BinaryTree) add_subdirectory(Breakpad) add_subdirectory(ByteOrder) add_subdirectory(CountDownLatch) +if(crashpad_FOUND) + add_subdirectory(Crashpad) +endif() add_subdirectory(Curl) add_subdirectory(DesignPattern) add_subdirectory(Glog) @@ -113,12 +106,31 @@ add_subdirectory(Mutex) add_subdirectory(OpenSSL) add_subdirectory(Thread) -if(CMAKE_HOST_WIN32) - -elseif(CMAKE_HOST_APPLE) - -elseif(CMAKE_HOST_UNIX) +if(CMAKE_HOST_LINUX) add_subdirectory(Client) add_subdirectory(Icmp) add_subdirectory(Server) endif() + +# 输出 CMake 版本和构建系统类型 +message("CMake Version: ${CMAKE_VERSION}") +message("Generator: ${CMAKE_GENERATOR}") + +# 输出编译器信息 +message("C Compiler ID: ${CMAKE_C_COMPILER_ID}") +message("C++ Compiler ID: ${CMAKE_CXX_COMPILER_ID}") +message("C++ Compiler Version: ${CMAKE_CXX_COMPILER_VERSION}") + +# 输出构建类型和编译选项 +message("Build Type: ${CMAKE_BUILD_TYPE}") +message("C++ Compiler Flags: ${CMAKE_CXX_FLAGS}") + +# 输出链接选项 +message("Executable Linker Flags: ${CMAKE_EXE_LINKER_FLAGS}") + +# 输出构建和源代码目录 +message("Build Directory: ${CMAKE_BINARY_DIR}") +message("Source Directory: ${CMAKE_SOURCE_DIR}") + +# 输出目标架构 +message("Target Processor: ${CMAKE_SYSTEM_PROCESSOR}") diff --git a/CountDownLatch/countdownlatch.hpp b/CountDownLatch/countdownlatch.hpp index 1584751..c6b17b3 100644 --- a/CountDownLatch/countdownlatch.hpp +++ b/CountDownLatch/countdownlatch.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include diff --git a/Crashpad/CMakeLists.txt b/Crashpad/CMakeLists.txt new file mode 100644 index 0000000..a59f4d8 --- /dev/null +++ b/Crashpad/CMakeLists.txt @@ -0,0 +1,13 @@ +add_executable(Crashpad crashpad.cc crashpad.hpp main.cc) +target_link_libraries(Crashpad PRIVATE crashpad::crashpad) + +string(REPLACE "share" "tools" crash_handler_path ${crashpad_DIR}) +message(STATUS "crashpad tools directory: ${crash_handler_path}") + +add_custom_command( + TARGET Crashpad + POST_BUILD + COMMAND ${CMAKE_COMMAND} -E make_directory + "$/crashpad/" + COMMAND ${CMAKE_COMMAND} -E copy_directory ${crash_handler_path}/ + "$/crashpad") diff --git a/Crashpad/crashpad.cc b/Crashpad/crashpad.cc new file mode 100644 index 0000000..5882f25 --- /dev/null +++ b/Crashpad/crashpad.cc @@ -0,0 +1,59 @@ +#include "crashpad.hpp" + +#include +#include +#include + +#ifdef _WIN32 +auto convertStringToWideString(const std::string &str) -> std::wstring +{ + if (str.empty()) { + return {}; + } + + // 首先,获取转换后的字符串长度(不包括空终止符) + int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0); + + // 如果转换失败,返回空字符串 + if (len == 0) { + return {}; + } + + // 分配足够的空间来存储转换后的字符串 + std::wstring wstr(len, 0); + + // 执行转换 + MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &wstr[0], len); + + // 去除末尾的空字符 + wstr.resize(len - 1); + return wstr; +} +#endif + +Crashpad::Crashpad(const std::string &dumpPath, + const std::string &libexecPath, + const std::string &reportUrl, + bool crashReportingEnabled) +{ + auto handlerPath = libexecPath + "/crashpad_handler"; +#ifdef _WIN32 + handlerPath += ".exe"; + base::FilePath database(convertStringToWideString(dumpPath)); + base::FilePath handler(convertStringToWideString(handlerPath)); +#else + base::FilePath database(dumpPath); + base::FilePath handler(handlerPath); +#endif + + auto dbPtr = crashpad::CrashReportDatabase::Initialize(database); + if (dbPtr && (dbPtr->GetSettings() != nullptr)) { + dbPtr->GetSettings()->SetUploadsEnabled(crashReportingEnabled); + } + + m_crashpadClientPtr = std::make_unique(); + m_crashpadClientPtr + ->StartHandler(handler, database, database, reportUrl, {}, {"--no-rate-limit"}, true, true); +} + +Crashpad::~Crashpad() = default; diff --git a/Crashpad/crashpad.hpp b/Crashpad/crashpad.hpp new file mode 100644 index 0000000..cd63fe9 --- /dev/null +++ b/Crashpad/crashpad.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include + +#include +#include + +namespace crashpad { +class CrashpadClient; +} // namespace crashpad + +class Crashpad : noncopyable +{ +public: + explicit Crashpad(const std::string &dumpPath, + const std::string &libexecPath, + const std::string &reportUrl, + bool crashReportingEnabled); + ~Crashpad(); + +private: + std::unique_ptr m_crashpadClientPtr; +}; diff --git a/Crashpad/main.cc b/Crashpad/main.cc new file mode 100644 index 0000000..548c24b --- /dev/null +++ b/Crashpad/main.cc @@ -0,0 +1,23 @@ +#include "crashpad.hpp" + +#include + +void crash() +{ + int *p = nullptr; + *p = 1; +} + +auto main() -> int +{ + auto dumpPath = std::filesystem::current_path() / "crashpad"; + if (!std::filesystem::exists(dumpPath)) { + std::filesystem::create_directory(dumpPath); + } + + Crashpad crashpad(dumpPath.string(), dumpPath.string(), "http://127.0.0.1:8080", true); + + crash(); + + return 0; +} diff --git a/Curl/httpclient.hpp b/Curl/httpclient.hpp index e2a9bf0..aa52a76 100644 --- a/Curl/httpclient.hpp +++ b/Curl/httpclient.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include diff --git a/Curl/httpclient_async.hpp b/Curl/httpclient_async.hpp index f939b56..e3fff2f 100644 --- a/Curl/httpclient_async.hpp +++ b/Curl/httpclient_async.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -136,7 +136,7 @@ class HttpClientAsync : noncopyable { CURLMsg *message = nullptr; int pending = 0; - while ((message = curl_multi_info_read(m_multi, &pending))) { + while ((message = curl_multi_info_read(m_multi, &pending)) != nullptr) { switch (message->msg) { case CURLMSG_DONE: { CURL *handle = message->easy_handle; diff --git a/Curl/tcpclient.cc b/Curl/tcpclient.cc index 57b749c..3d2be5e 100644 --- a/Curl/tcpclient.cc +++ b/Curl/tcpclient.cc @@ -28,7 +28,7 @@ auto TcpClient::connect() -> bool } m_curl = curl_easy_init(); - if (!m_curl) { + if (m_curl == nullptr) { return false; } diff --git a/Curl/tcpclient.hpp b/Curl/tcpclient.hpp index 7d8bacf..b180056 100644 --- a/Curl/tcpclient.hpp +++ b/Curl/tcpclient.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include diff --git a/DesignPattern/Singleton/singleton.hpp b/DesignPattern/Singleton/singleton.hpp index 6846ced..b3e845e 100644 --- a/DesignPattern/Singleton/singleton.hpp +++ b/DesignPattern/Singleton/singleton.hpp @@ -1,7 +1,7 @@ #ifndef SINGLETON_HPP #define SINGLETON_HPP -#include +#include #include #include diff --git a/Glog/main.cc b/Glog/main.cc index 4313109..2762e79 100644 --- a/Glog/main.cc +++ b/Glog/main.cc @@ -6,18 +6,18 @@ auto main(int argc, char **argv) -> int { (void) argc; - const auto *log_dir = "./glog_demo"; + const auto log_dir = std::filesystem::current_path() / "glog"; // Initialize Google’s logging library. google::InitGoogleLogging(argv[0]); google::InstallFailureSignalHandler(); google::SetLogFilenameExtension(".log"); - google::EnableLogCleaner(7); + google::EnableLogCleaner(std::chrono::hours(24 * 7)); //google::DisableLogCleaner(); FLAGS_alsologtostderr = true; // 是否将日志输出到文件和stderr FLAGS_colorlogtostderr = true; // 是否启用不同颜色显示 FLAGS_max_log_size = 1000; // 最大日志文件大小 - fLS::FLAGS_log_dir = log_dir; + fLS::FLAGS_log_dir = log_dir.string(); std::filesystem::create_directories(log_dir); diff --git a/Icmp/icmp.hpp b/Icmp/icmp.hpp index 58faaa2..87cfb4a 100644 --- a/Icmp/icmp.hpp +++ b/Icmp/icmp.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include diff --git a/MonitorDir/CMakeLists.txt b/MonitorDir/CMakeLists.txt index 737efbb..bb87097 100644 --- a/MonitorDir/CMakeLists.txt +++ b/MonitorDir/CMakeLists.txt @@ -5,7 +5,7 @@ elseif(CMAKE_HOST_APPLE) add_executable(MonitorDir main.cc monitordir_mac.cc monitordir.cc monitordir.hpp) target_link_libraries(MonitorDir "-framework CoreServices") -elseif(CMAKE_HOST_UNIX) +elseif(CMAKE_HOST_LINUX) add_executable(MonitorDir main.cc monitordir_linux.cc monitordir.cc monitordir.hpp) endif() diff --git a/MonitorDir/monitordir.hpp b/MonitorDir/monitordir.hpp index 51a66eb..36fe05b 100644 --- a/MonitorDir/monitordir.hpp +++ b/MonitorDir/monitordir.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include diff --git a/Mutex/mutex.hpp b/Mutex/mutex.hpp index 3e367c8..5bf32d1 100644 --- a/Mutex/mutex.hpp +++ b/Mutex/mutex.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include diff --git a/OpenSSL/openssl_rsa.cc b/OpenSSL/openssl_rsa.cc index dc55489..a5c2463 100644 --- a/OpenSSL/openssl_rsa.cc +++ b/OpenSSL/openssl_rsa.cc @@ -1,6 +1,6 @@ #include "openssl_utils.hpp" -#include +#include #include #include diff --git a/README.md b/README.md index 318b64c..f191962 100644 --- a/README.md +++ b/README.md @@ -16,22 +16,24 @@ 5. [ByteOrder](/ByteOrder/byteorder.hpp)——判断系统的字节序; 6. [Client](/Client/client.cpp)——一个简单的Linux select socket客户端; 7. [CountDownLatch](/CountDownLatch/countdownlatch.hpp)——使用std::mutex和std::condition_variable实现的简单倒计时门闩(std::latch c++20); -8. [Curl](/Curl/)——curl的简单使用; - 1. [TcpClient](/Curl/tcpclient.hpp)——使用curl实现的简单tcp客户端; - 2. [HttpClient](/Curl/httpclient.hpp)——使用curl实现的简单http同步客户端; - 3. [HttpClientAsync](/Curl/httpclient_async.hpp)——使用curl实现的简单http异步客户端; -9. [DesignPattern](/DesignPattern)——设计模式的一些例子; - 1. [Factory](/DesignPattern/Factory/factory.hpp)——工厂模式; - 2. [MVC](/DesignPattern/MVC/model.hpp)——mvc模式; - 3. [Observer](/DesignPattern/Observer/observer.hpp)——观察者模式; - 4. [Singleton](/DesignPattern/Singleton/singleton.hpp)——单例模式; -10. [Glog](/Glog/main.cc)——google glog的例子; -11. [Icmp](/Icmp/icmp.hpp)——linux icmp协议的简单封装; -12. [LinkedList](/LinkedList/linkedlist.hpp)——链表的相关操作,插入、移除、反转、打印; -13. [Memcpy](/Memcpy/memcpy.hpp)——`memcpy`函数实现; -14. [MonitorDir](/MonitorDir/monitordir.hpp)——windows(`ReadDirectoryChangesW`),macos(`FSEvents`)和linux(`inotify`)目录监控的简单例子; -15. [Mutex](/Mutex/mutex.hpp)——使用std::atomic_flag实现的简单互斥锁和自旋锁; -16. [OpenSSL](/OpenSSL)——openssl的一些例子; +8. [Crashpad](/Crashpad/crashpad.hpp)——crashpad的简单封装; + 1. vcpkg对google crashpad在macos和linux上的支持并不好; +9. [Curl](/Curl/)——curl的简单使用; +1. [TcpClient](/Curl/tcpclient.hpp)——使用curl实现的简单tcp客户端; +2. [HttpClient](/Curl/httpclient.hpp)——使用curl实现的简单http同步客户端; +3. [HttpClientAsync](/Curl/httpclient_async.hpp)——使用curl实现的简单http异步客户端; +10. [DesignPattern](/DesignPattern)——设计模式的一些例子; +11. [Factory](/DesignPattern/Factory/factory.hpp)——工厂模式; +12. [MVC](/DesignPattern/MVC/model.hpp)——mvc模式; +13. [Observer](/DesignPattern/Observer/observer.hpp)——观察者模式; +14. [Singleton](/DesignPattern/Singleton/singleton.hpp)——单例模式; +15. [Glog](/Glog/main.cc)——google glog的例子; +16. [Icmp](/Icmp/icmp.hpp)——linux icmp协议的简单封装; +17. [LinkedList](/LinkedList/linkedlist.hpp)——链表的相关操作,插入、移除、反转、打印; +18. [Memcpy](/Memcpy/memcpy.hpp)——`memcpy`函数实现; +19. [MonitorDir](/MonitorDir/monitordir.hpp)——windows(`ReadDirectoryChangesW`),macos(`FSEvents`)和linux(`inotify`)目录监控的简单例子; +20. [Mutex](/Mutex/mutex.hpp)——使用std::atomic_flag实现的简单互斥锁和自旋锁; +21. [OpenSSL](/OpenSSL)——openssl的一些例子; 1. [aes](/OpenSSL/openssl_aes.cc)——aes加解密的例子; 2. [base64](/OpenSSL/openssl_base64.cc)——base64编解码的例子; 3. [hash](/OpenSSL/openssl_hash.cc)——sha256的例子; @@ -41,10 +43,10 @@ 7. [sm4](/OpenSSL/openssl_sm4.cc)——sm4加解密的例子; 8. [x509](/OpenSSL/openssl_x509.cc)——x509证书的例子; 9. [bash](/OpenSSL/openssl_bash.sh)——openssl命令行的例子; -17. [Server](/Server)——linux server的一些例子; +22. [Server](/Server)——linux server的一些例子; 1. [server_epoll](/Server/server_epoll.cc)——epoll的例子; 2. [server_poll](/Server/server_poll.cc)——poll的例子; 3. [server_select](/Server/server_select.cc)——select的例子; -18. [Thread](/Thread/)——基于std::thread实现的线程类,包括线程池; +23. [Thread](/Thread/)——基于std::thread实现的线程类,包括线程池; 1. [Thread](/Thread/thread.hpp)——线程类; 2. [ThreadPool](/Thread/threadpool.hpp)——线程池; diff --git a/Thread/thread.hpp b/Thread/thread.hpp index a53d5d2..2a75916 100644 --- a/Thread/thread.hpp +++ b/Thread/thread.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -16,7 +16,7 @@ class Thread : noncopyable using Task = std::function; Thread() = default; - Thread(Task task) { setTask(task); } + explicit Thread(Task task) { setTask(task); } ~Thread() { stop(); } void setTask(Task task) { m_task = std::move(task); } @@ -46,9 +46,9 @@ class Thread : noncopyable m_condition.wait(lock, [this]() { return m_running.load(); }); } - bool isRunning() const { return m_running; } + auto isRunning() const -> bool { return m_running; } - std::thread::id getThreadId() const { return m_thread.get_id(); } + auto getThreadId() const -> std::thread::id { return m_thread.get_id(); } static void yield() { std::this_thread::yield(); } @@ -62,7 +62,7 @@ class Thread : noncopyable std::this_thread::sleep_until(timePoint); } - static unsigned int hardwareConcurrency() + static auto hardwareConcurrency() -> unsigned int { unsigned int n = std::thread::hardware_concurrency(); // 如果不支持,返回0 assert(n > 0); diff --git a/object.hpp b/utils/object.hpp similarity index 100% rename from object.hpp rename to utils/object.hpp diff --git a/scopeguard.hpp b/utils/scopeguard.hpp similarity index 100% rename from scopeguard.hpp rename to utils/scopeguard.hpp diff --git a/vcpkg.json b/vcpkg.json index 4900b9e..570d9cc 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -23,7 +23,11 @@ "http2", "tool" ] + }, + { + "name": "crashpad", + "platform": "windows" } ], - "builtin-baseline": "3c76dc55f8bd2b7f4824bcd860055094bfbbb9ea" + "builtin-baseline": "ad3bae57455a3c3ce528fcd47d8e8027d0498add" } \ No newline at end of file