C++任务队列

Task.h

cpp 复制代码
#pragma once
class CTask
{
	int* m_nCount;
public:
	CTask(int* nCount);
	~CTask();

	void DoWork();//具体的任务
};

Task.cpp

cpp 复制代码
#include "task.h"
#include <iostream>
using namespace std;

CTask::CTask(int* nCount)
{
	m_nCount = nCount;
}

CTask::~CTask()
{

}

void CTask::DoWork()
{
	(*m_nCount)++;
	 cout << "Count = " << *m_nCount << endl;
}

TaskQueue.h

cpp 复制代码
 #pragma once
 #include "Task.h"
 #include <queue>
 #include <mutex>
 #include <thread>

 class CTaskQueue
 {
 public:
	     CTaskQueue();
	     ~CTaskQueue();
	
 private:
	     std::queue<CTask*> m_taskQueue;  //任务队列
	     std::thread m_thread;
	     std::mutex m_mutex;
	     bool m_bIsStart;   //线程是否开启
	
 public:
	     //工作线程
		 void WorkThread();
	
		 //向任务队列添加任务
		 bool Push(CTask * task);
	     //从任务队列获取任务
		 CTask * Pop();
	
		 //开启线程
		 bool Start();
	     //终止线程
		 void Stop();
};

TaskQueue.cpp

cpp 复制代码
#include "TaskQueue.h"
#include <iostream>
CTaskQueue::CTaskQueue()
{
}

CTaskQueue::~CTaskQueue()
{
}

void CTaskQueue::WorkThread()
{
   while (m_bIsStart)
   {
     if (!m_taskQueue.empty())
     {
       CTask * it = m_taskQueue.front();
       it->DoWork();
       m_taskQueue.pop();
       delete it;
     }
   }
}

bool CTaskQueue::Push(CTask* task)
{
    if (task == nullptr)
    {
     return false;
    }

    m_mutex.lock();
    m_taskQueue.push(task);
    m_mutex.unlock();
    return true;
}

CTask* CTaskQueue::Pop()
{
    CTask* it = nullptr;

    m_mutex.lock();
    if (!m_taskQueue.empty())
    {
        it = m_taskQueue.front();
        m_taskQueue.pop();
    }
    m_mutex.unlock();
    return it;
}

bool CTaskQueue::Start()
{
    if (m_bIsStart)
    {
        return false;
    }
    m_bIsStart = true;
    //开启工作线程
    m_thread = std::thread(&CTaskQueue::WorkThread, this);
    return true;
}

void CTaskQueue::Stop()
{
    m_bIsStart = false;
    m_thread.join();
}

main.cpp

cpp 复制代码
#include "TaskQueue.h"
#include "Task.h"

void MyWorkTask1(CTaskQueue * pTaskQueue, int* nCount)
{
	     for (size_t i = 0; i < 20; i++)
		     {
		         CTask * task = new CTask(nCount);
		         pTaskQueue->Push(task);
		     }
}

 void MyWorkTask2(CTaskQueue * pTaskQueue, int* nCount)
 {
	    for (size_t i = 0; i < 20; i++)
		    {
		        CTask * task = new CTask(nCount);
		        pTaskQueue->Push(task);
		    }
 }

int main()
{
	   CTaskQueue * pTaskQueue = new CTaskQueue();
	   pTaskQueue->Start();
	
	   int* nCount = new int(0);

	   std::thread thread1(&MyWorkTask1, pTaskQueue, nCount);
	   std::thread thread2(&MyWorkTask2, pTaskQueue, nCount);
	
		//等待线程结束
		if (thread1.joinable())
		{
		      thread1.join();
		}
	    if (thread2.joinable())
		{
		        thread2.join();
		}
	
		system("pause");
	    return 0;
}
相关推荐
程序喵大人6 分钟前
【C++进阶】STL容器与迭代器 - 10 把容器、迭代器和数据流串成一个小程序
开发语言·c++·容器·小程序·迭代器·stl
luj_176827 分钟前
随机性在算法与占卜中的共通原理
c语言·开发语言·c++·经验分享·算法
Lazionr34 分钟前
vector初识——从动态数组到STL核心容器
开发语言·c++
yyds_yyd_1008639 分钟前
3731. 找出缺失的元素(2026.08.04)
c++·leetcode
呱呱巨基1 小时前
CMake基础
linux·c++·笔记·学习
Tri_Function13 小时前
动态规划DP1(c++)
c++
清水迎朝阳15 小时前
客户端软件 — 用户统计方案
服务器·c++·客户端·用户统计
再卷也是菜17 小时前
C++17(下)
c++
alexwang21117 小时前
HDU 4348 详细题解
c++·算法·题解·hdu·主席树·可持久化数据结构·可持久化线段树
程序员zgh17 小时前
C++ 拷贝赋值运算符 详解
c语言·开发语言·c++