no message
parent
7443d203fb
commit
b893e33b4c
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
|
@ -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"
|
||||
}
|
||||
}
|
|
@ -73,16 +73,16 @@ std::condition_variable gCv; // 全局条件变量.
|
|||
bool gReady = false; // 全局标志位.
|
||||
void do_print_id(int id){
|
||||
std::unique_lock <std::mutex> lck(cMtx);
|
||||
while (!gReady) // 如果标志位不为 true, 则等待...
|
||||
while (!gReady)
|
||||
gCv.wait(lck); // 当前线程被阻塞, 当全局标志位变为 true 之后,
|
||||
// 线程被唤醒, 继续往下执行打印线程编号id.
|
||||
std::cout << "thread " << id << '\n';
|
||||
}
|
||||
|
||||
void go(){
|
||||
std::unique_lock <std::mutex> 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;
|
||||
|
|
|
@ -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(){
|
||||
|
||||
}
|
|
@ -1 +1,60 @@
|
|||
#include "threadpool.h"
|
||||
#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<std::mutex> 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();
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -3,17 +3,69 @@
|
|||
#include <mutex>
|
||||
#include <atomic>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
#include <condition_variable>
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
|
||||
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<std::thread> mThreads;
|
||||
}ThreadPool;
|
||||
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<std::thread*> mThreads;
|
||||
std::queue<Task *> mTasks;
|
||||
std::mutex mMutex;
|
||||
std::condition_variable mCd;
|
||||
}ThreadPool;
|
||||
}
|
Loading…
Reference in New Issue