From b893e33b4cfc289ef4f8ba809582389ae620e865 Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Thu, 11 Mar 2021 18:07:29 +0800 Subject: [PATCH] no message --- test/src/cpp11/.vscode/c_cpp_properties.json | 23 ------ test/src/cpp11/.vscode/launch.json | 27 ------- test/src/cpp11/.vscode/settings.json | 57 --------------- test/src/cpp11/thread_usage.cpp | 12 ++-- test/src/cpp11/threadpool.c | 18 ----- test/src/cpp11/threadpool.cpp | 61 +++++++++++++++- test/src/cpp11/threadpool.h | 76 ++++++++++++++++---- 7 files changed, 131 insertions(+), 143 deletions(-) delete mode 100644 test/src/cpp11/.vscode/c_cpp_properties.json delete mode 100644 test/src/cpp11/.vscode/launch.json delete mode 100644 test/src/cpp11/.vscode/settings.json delete mode 100644 test/src/cpp11/threadpool.c diff --git a/test/src/cpp11/.vscode/c_cpp_properties.json b/test/src/cpp11/.vscode/c_cpp_properties.json deleted file mode 100644 index ec9f159..0000000 --- a/test/src/cpp11/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "configurations": [ - { - "name": "Win32", - "includePath": [ - "${workspaceFolder}/**" - ], - "defines": [ - "_DEBUG", - "UNICODE", - "_UNICODE" - ], - "windowsSdkVersion": "10.0.17763.0", - "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe", - "cStandard": "c17", - "cppStandard": "c++17", - "intelliSenseMode": "windows-msvc-x64", - "configurationProvider": "ms-vscode.cmake-tools", - "compileCommands": "${workspaceFolder}/build/compile_commands.json" - } - ], - "version": 4 -} \ No newline at end of file diff --git a/test/src/cpp11/.vscode/launch.json b/test/src/cpp11/.vscode/launch.json deleted file mode 100644 index ce05d7a..0000000 --- a/test/src/cpp11/.vscode/launch.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "C++ Launch (GDB)", - "type": "cppdbg", - "request": "launch", - "program": "cpp11.exe", - "args": [], - "stopAtEntry": false, - "cwd": "${workspaceFolder}", - "environment": [], - "externalConsole": false, - "MIMode": "gdb", - "setupCommands": [ - { - "description": "为 gdb 启用整齐打印", - "text": "-enable-pretty-printing", - "ignoreFailures": true - } - ] - } - ] -} \ No newline at end of file diff --git a/test/src/cpp11/.vscode/settings.json b/test/src/cpp11/.vscode/settings.json deleted file mode 100644 index a1b54cd..0000000 --- a/test/src/cpp11/.vscode/settings.json +++ /dev/null @@ -1,57 +0,0 @@ -{ - "files.associations": { - "ostream": "cpp", - "cstdint": "cpp", - "future": "cpp", - "thread": "cpp", - "iostream": "cpp", - "algorithm": "cpp", - "atomic": "cpp", - "chrono": "cpp", - "cmath": "cpp", - "condition_variable": "cpp", - "cstddef": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "cwchar": "cpp", - "exception": "cpp", - "resumable": "cpp", - "functional": "cpp", - "initializer_list": "cpp", - "ios": "cpp", - "iosfwd": "cpp", - "istream": "cpp", - "limits": "cpp", - "list": "cpp", - "memory": "cpp", - "mutex": "cpp", - "new": "cpp", - "ratio": "cpp", - "stdexcept": "cpp", - "streambuf": "cpp", - "system_error": "cpp", - "xthread": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "typeinfo": "cpp", - "unordered_map": "cpp", - "utility": "cpp", - "vector": "cpp", - "xfacet": "cpp", - "xhash": "cpp", - "xiosbase": "cpp", - "xlocale": "cpp", - "xlocinfo": "cpp", - "xlocnum": "cpp", - "xmemory": "cpp", - "xmemory0": "cpp", - "xstddef": "cpp", - "xstring": "cpp", - "xtr1common": "cpp", - "xutility": "cpp", - "ctime": "cpp", - "threadpool.h": "c", - "*.rh": "cpp" - } -} \ No newline at end of file diff --git a/test/src/cpp11/thread_usage.cpp b/test/src/cpp11/thread_usage.cpp index 75d5ffb..c8f9d22 100644 --- a/test/src/cpp11/thread_usage.cpp +++ b/test/src/cpp11/thread_usage.cpp @@ -73,16 +73,16 @@ std::condition_variable gCv; // 全局条件变量. bool gReady = false; // 全局标志位. void do_print_id(int id){ std::unique_lock lck(cMtx); - while (!gReady) // 如果标志位不为 true, 则等待... + while (!gReady) gCv.wait(lck); // 当前线程被阻塞, 当全局标志位变为 true 之后, // 线程被唤醒, 继续往下执行打印线程编号id. std::cout << "thread " << id << '\n'; } void go(){ - std::unique_lock lck(cMtx); - gReady = true; // 设置全局标志位为 true. - gCv.notify_one(); // 唤醒所有线程. + gReady = true; + gCv.notify_all(); // 唤醒所有线程. + } int TestConditionVariable() { @@ -91,7 +91,9 @@ int TestConditionVariable() { for (int i = 0; i < 10; ++i) threads[i] = std::thread(do_print_id, i); std::cout << "10 threads ready to race...\n"; - go(); // go! + go(); + go(); + for (auto & th:threads) th.join(); return 0; diff --git a/test/src/cpp11/threadpool.c b/test/src/cpp11/threadpool.c deleted file mode 100644 index e6d861c..0000000 --- a/test/src/cpp11/threadpool.c +++ /dev/null @@ -1,18 +0,0 @@ -#include "threadpool.h" - -int ThreadPool::AddTask(){ - -} - -void ThreadPool::Start(){ - -} - -void ThreadPool::Stop(){ - for (auto & th:threads) - th.join(); -} - -bool ThreadPool::Started(){ - -} \ No newline at end of file diff --git a/test/src/cpp11/threadpool.cpp b/test/src/cpp11/threadpool.cpp index 655ef9e..cf63c79 100644 --- a/test/src/cpp11/threadpool.cpp +++ b/test/src/cpp11/threadpool.cpp @@ -1 +1,60 @@ -#include "threadpool.h" \ No newline at end of file +#include "threadpool.h" + +namespace general{ + void work(general::CThreadPool *pool){ + + } + CThreadPool::CThreadPool(int num) + { + this->mThreadCnt = num; + for(int i = 0;i < num;i++){ + std::thread *t = new std::thread(general::work,this); + this->mThreads.push_back(t); + } + } + int CThreadPool::AddTask(Task *t){ + if ( nullptr == t){ + return -1; + } + std::unique_lock lck(this->mMutex); + this->mTasks.push(t); + } + + void CThreadPool::Start(){ + + } + + Task *CThreadPool::PopTask(){ + if(!this->mTasks.empty()){ + return this->mTasks.front(); + this->mTasks.pop(); + } + } + + void CThreadPool::Stop(){ + mStarted = false; + for (size_t i = 0; i != this->mThreads.size(); ++i) + { + // if (mThreads[i].joinable()) + // { + // mThreads[i].join(); + // } + } + } + + bool CThreadPool::Started(){ + return this->mStarted; + } + CThreadPool::~CThreadPool(){ + if(this->mStarted){ + for (size_t i = 0; i != this->mThreads.size(); ++i) + { + // if (mThreads[i].joinable()) + // { + // mThreads[i].join(); + // } + } + } + } + +} \ No newline at end of file diff --git a/test/src/cpp11/threadpool.h b/test/src/cpp11/threadpool.h index 2a33139..a641352 100644 --- a/test/src/cpp11/threadpool.h +++ b/test/src/cpp11/threadpool.h @@ -3,17 +3,69 @@ #include #include #include +#include +#include +#include +#include +#include -using namespace std; +namespace general{ + class CThreadPool; + enum PRIORITY + { + MIN = 1, NORMAL = 25, MAX = 50 + }; + typedef class CTask{ + public: + CTask() { + } + void SetPriority(int priority) { + if (priority>(PRIORITY::MAX)) + { + priority = (PRIORITY::MAX); + } + else if (priority>(PRIORITY::MAX)) + { + priority = (PRIORITY::MIN); + } + } + virtual void Run() = 0; + protected: + int priority_; + }Task; -typedef class CThreadPool { -public: - CThreadPool(int num); - int AddTask(); - void Start(); - void Stop(); - bool Started(); -private: - bool mStarted; - std::vector mThreads; -}ThreadPool; \ No newline at end of file + class Worker{ + public: + // The required constructor for this example. + explicit Worker(int& evenCount) + : m_evenCount(evenCount) { } + + void operator()(CThreadPool *pool) const { + + } + private: + // Default assignment operator to silence warning C4512. + Worker& operator=(const Worker&); + int& m_evenCount; + }; + + typedef class CThreadPool { + public: + CThreadPool(int num); + ~CThreadPool(); + int AddTask(Task *); + void Start(); + void Stop(); + bool Started(); + Task *PopTask(); + private: + CThreadPool(); + void work(void*); + uint32_t mThreadCnt; + bool mStarted; + std::vector mThreads; + std::queue mTasks; + std::mutex mMutex; + std::condition_variable mCd; + }ThreadPool; +} \ No newline at end of file