C++ 简单的任务队列

任务队列是指能够实现任务在多线程间安全传递的先入先出的队列。

任务是指组合了数据和操作的对象,这里面定义为CTask类的对象。

任务的实现:

Task.cpp
C++ 简单的任务队列C++ 简单的任务队列

1 #include "stdafx.h"
 2 #include "Task.h"
 3 #include <iostream>
 4 using namespace std;
 5 
 6 CTask::CTask(int* nCount)
 7 {
 8     m_nCount = nCount;
 9 }
10 
11 CTask::~CTask()
12 {
13 }
14 
15 void CTask::DoWork()
16 {
17     (*m_nCount)++;
18 
19     cout << "Count = " << *m_nCount << endl;
20 }

View Code

Task.h
C++ 简单的任务队列C++ 简单的任务队列

1 #pragma once
 2 class CTask
 3 {
 4     int* m_nCount;
 5 public:
 6     CTask(int* nCount);
 7     ~CTask();
 8 
 9     void DoWork();
10 };

View Code

队列的实现:

TaskQueue.cpp
C++ 简单的任务队列C++ 简单的任务队列

1 #include "stdafx.h"
 2 #include "TaskQueue.h"
 3 
 4 
 5 CTaskQueue::CTaskQueue()
 6 {
 7 }
 8 
 9 
10 CTaskQueue::~CTaskQueue()
11 {
12 }
13 
14 
15 //工作线程
16 void CTaskQueue::WorkThread()
17 {
18     while (m_bIsStart)
19     {
20         if (!m_taskQueue.empty())
21         {
22             CTask* it = m_taskQueue.front();
23             it->DoWork();
24             m_taskQueue.pop();
25             delete it;
26         }
27     }
28 }
29 
30 //向任务队列添加任务
31 bool CTaskQueue::Push(CTask* task)
32 {
33     if (task == nullptr)
34     {
35         return false;
36     }
37 
38     m_mutex.lock();
39     m_taskQueue.push(task);
40     m_mutex.unlock();
41 
42     return true;
43 }
44 //从任务队列获取任务
45 CTask* CTaskQueue::Pop()
46 {
47     CTask* it = nullptr;
48 
49     m_mutex.lock();
50     if (!m_taskQueue.empty())
51     {
52         it = m_taskQueue.front();
53         m_taskQueue.pop();
54     }
55     m_mutex.unlock();
56     return it;
57 }
58 
59 bool CTaskQueue::Start()
60 {
61     if (m_bIsStart)
62     {
63         return false;
64     }
65     m_bIsStart = true;
66     m_thread = std::thread(&CTaskQueue::WorkThread, this);
67 
68     return true;
69 }
70 
71 void CTaskQueue::Stop()
72 {
73     m_bIsStart = false;
74     m_thread.join();
75 }

View Code

*TaskQueue*.h
C++ 简单的任务队列C++ 简单的任务队列

1 #pragma once
 2 #include "Task.h"
 3 #include <queue>
 4 #include <mutex>
 5 #include <thread>
 6 
 7 class CTaskQueue
 8 {
 9 public:
10     CTaskQueue();
11     ~CTaskQueue();
12 
13 private:
14     std::queue<CTask*> m_taskQueue;  //任务队列
15     std::thread m_thread;
16     std::mutex m_mutex;
17     bool m_bIsStart;   //线程是否开启
18 
19 public:
20     //工作线程
21     void WorkThread();  
22 
23     //向任务队列添加任务
24     bool Push(CTask* task);
25     //从任务队列获取任务
26     CTask* Pop();
27 
28     //开启线程
29     bool Start();
30     //终止线程
31     void Stop();
32 };

View Code

测试demo:
C++ 简单的任务队列C++ 简单的任务队列

1 // TaskQueue.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include "TaskQueue.h"
 6 #include "Task.h"
 7 
 8 void MyWorkTask1(CTaskQueue* pTaskQueue, int* nCount)
 9 {
10     for (size_t i = 0; i < 20; i++)
11     {    
12         CTask* task = new CTask(nCount);
13         pTaskQueue->Push(task);
14     }
15 }
16 
17 void MyWorkTask2(CTaskQueue* pTaskQueue, int* nCount)
18 {
19     for (size_t i = 0; i < 20; i++)
20     {
21         CTask* task = new CTask(nCount);
22         pTaskQueue->Push(task);
23     }
24 }
25 
26 int _tmain(int argc, _TCHAR* argv[])
27 {
28     CTaskQueue* pTaskQueue = new CTaskQueue();
29     pTaskQueue->Start();
30 
31     int* nCount = new  int(0);
32 
33     std::thread thread1(&MyWorkTask1, pTaskQueue, nCount);
34     std::thread thread2(&MyWorkTask2, pTaskQueue, nCount);
35 
36     //等待线程结束
37     if (thread1.joinable())
38     {
39         thread1.join();
40     }
41     if (thread2.joinable())
42     {
43         thread2.join();
44     }
45 
46     system("pause");
47     return 0;
48 }

View Code

原文链接: https://www.cnblogs.com/xiaobaiobject/p/TaskQueue.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/239891

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午8:15
下一篇 2023年2月13日 下午8:16

相关推荐