c++多线程队列的实现

  本人大四,几乎所有的同学都参加了实习,今天是参加工作的第20天,发一个多线程队列给大家看一下,虽然比较简单,但是还是希望能帮到需要的人。

下面这个链接是我帮一个同学推广的,希望大家能帮忙点一下,3Q。http://www.2345.com/?kdevelop

  以下代码是在linux下kdevelop环境中编写的,测试通过。

  接下来是代码:(改了一下,加了个锁)

/*
*创建人:xcl 时间:2012.2.26
*说明:MyQueue类
*/
#ifndef MYQUEUE_H_
#define MYQUEUE_H_

#include <pthread.h>
#include <iostream>
using namespace std;

const int QUEUESIZE = 20;
//类模型--------------------------------------
class MyQueue
{ public:
MyQueue();
~MyQueue();
public:
void Push(int key);
int Pop();
int GetSize();
bool IsFull();
bool IsEmpty();
private:
int rear;
int front;
int size;
int list[QUEUESIZE];
pthread_mutex_t queMutex; //新加的锁
};
//构造函数-----------------------------------------
MyQueue::MyQueue()
{
rear=front=0;
size=QUEUESIZE;
pthread_mutex_lock(&queMutex);
pthread_mutex_init(&queMutex,NULL); //初始化锁
}
//入队操作----------------------------------------
void MyQueue::Push(int key)
{
pthread_mutex_lock(&queMutex);
if(IsFull())
{
cout << "queue is full!" << endl;
pthread_mutex_unlock(&queMutex);
return;
}
list[rear]=key;
cout << "正在入队:"<< key <<endl;
rear =(rear+1)%size;
pthread_mutex_unlock(&queMutex);
}
//出队操作-----------------------------------------
int MyQueue::Pop()
{
int temp;
pthread_mutex_lock(&queMutex);
if(IsEmpty())
{
cout << "the queue is empty!"<< endl;
pthread_mutex_unlock(&queMutex);
return false;
}
temp=list[front];
cout << "正在出队~:"<< temp <<endl;
front=(front+1)%size;
pthread_mutex_unlock(&queMutex);
return temp;
}
//获取队列实际大小-----------------------------------
int MyQueue::GetSize()
{
int realSize;
pthread_mutex_lock(&queMutex);
realSize=(rear-front)%size;
pthread_mutex_unlock(&queMutex);
return realSize;
}
//判断队列是否为满--------------------------------------
bool MyQueue::IsFull()
{
if((rear+1)%size==front)
return true;
else
return false;
}
//判断队列是否为空-------------------------------------
bool MyQueue::IsEmpty()
{
if(front==rear)
return true;
else
return false;
}
#endif

  以下是主程序代码:

/***************************************************************************
* Copyright (C) 2012 by root *
* root@localhost.xcl *
*************************************************************************
*/


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <iostream>
#include <cstdlib>
#include <MyQueue.h>
#include <pthread.h>
//解决方案:写个C函数,然后再C函数中调用C++对象的成员函数。
using namespace std;
MyQueue *Q; //全局对象;
void showSize() //统计队列中实际数据的个数
{
int size;
size=Q->GetSize();
cout << "当前队列拥有的数据是:" << size <<endl;
}
void *pushed(void *args)
{
for(int i = 0; i < 10; ++i)
{
Q->Push(i+10);
showSize();
sleep(1);
}
return NULL;
}
void* poped(void *args)
{
int popData;
for(int i = 0;i < 10;++i)
{
popData = Q->Pop();
showSize();
sleep(2);
}
return NULL;
}
void showHelp()
{
cout <<"功能: 多线程队列,同时出入队.\n\t实时统计当前队列中实际数据个数。\n"<<
"\t实际数据从10开始。\n\n"<< endl;
}

int main(int argc, char *argv[])
{
showHelp();

int size,ret_push,ret_pop;
Q = new MyQueue(); //实例化对象
pthread_t id_push; //入队线程
pthread_t id_pop; //出队线程
ret_push = pthread_create(&id_push,NULL,pushed,NULL);

ret_pop = pthread_create(&id_pop,NULL,poped,NULL);

pthread_join(id_push,NULL);

pthread_join(id_pop,NULL);
return EXIT_SUCCESS;
}

本人新手,有什么错误希望大家指出,我现在学习c++刚十天,还经常被搞得很乱。一同学习,一起进步,yeah.

网站导航

 

原文链接: https://www.cnblogs.com/matizz/archive/2012/03/08/MyQueue.html

欢迎关注

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

    c++多线程队列的实现

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

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

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

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

(0)
上一篇 2023年2月8日 下午8:18
下一篇 2023年2月8日 下午8:18

相关推荐