widget.cpp ( File view )

  • By geqiandebei 2014-05-01
  • View(s):14
  • Download(s):1
  • Point(s): 1
			/* 分享 交流 学习 进步
 * CopyRight @ geqiandebei 搁浅的贝
 * QQ: 1163925944
 */
#include "widget.h"
#include "ui_widget.h"
#include "QSemaphore"
#include "QThread"
#include "QMutex"
#include "Consumer.h"
#include "Producer.h"
#include "QTime"
#include "QWaitCondition"

int buffer_size=0; //缓冲池初始大小
QSemaphore toProduce; //  Enter之后从LineEdit中获取缓冲池大小
QSemaphore toConsume; //  默认为0
QStringList Product;

QWaitCondition buffer_full;
QWaitCondition buffer_empty;
QMutex     mutex;
QMutex     mutex2;
char name[]="GEQIANDEBEI";
const int name_length=11;
QString creat_no();  //生成产品编码

/*
 *希望大家注意下面注释的部分,是我原来写的代码,程序执行会有异常,各位可以花功夫分析一下错在何处。
 */
Producer::Producer()
{



}
void Producer::run()
{

    while(1)
    {

       // mutex.lock();
      //   if(toConsume.available()==buffer_size){
buffer_full.wait(&mutex);
}
       if(toConsume.available()==buffer_size){
mutex.lock(); buffer_full.wait(&mutex);mutex.unlock();
}
       toProduce.acquire();
       mutex2.lock();
       a=creat_no();
       Product.append(a);
       a="产品 "+a+" 被生产";
       mutex2.unlock();
        toConsume.release();
        buffer_empty.wakeAll();
      //  mutex.unlock();
        emit change(a,1);
        sleep(1);
    
}

}

void Consumer::run()
{

  while(1)
   {

      // mutex.lock();
      // if(toConsume.available()==0){
 buffer_empty.wait(&mutex);
}
       if(toProduce.available()==buffer_size){
mutex.lock(); buffer_empty.wait(&mutex);mutex.unlock();
}
        toConsume.acquire();
        mutex2.lock();
        a=Product.takeLast();
        a="产品 "+a+" 被消费";
        mutex2.unlock();
        toProduce.release();
        buffer_full.wakeAll();
      //  mutex.unlock();
        emit change(a,2);
        sleep(1);
    
}

}

Consumer::Consumer()
{

}
//******************************************************
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{

    ui->setupUi(this);
    ui->progressBar->setValue(0);
    connect(ui->lineEdit,SIGNAL(returnPressed()),this,SLOT(hundle()));

}

Widget::~Widget()
{

    int pc=plist.count();
    int cc=clist.count();
    for(int i=0;i<pc;i++)
    {

        plist.at(i)->terminate();
        plist.at(i)->wait();
    
}
    for(int i=0;i<cc;i++)
    {

        clist.at(i)->terminate();
        clist.at(i)->wait();
    
}
    delete ui;

}

void Widget::hundle()
{

    QString x=ui->lineEdit->text();
    if(x.isEmpty()) return;
    if(x.toInt())
    {

        buffer_size=x.toInt();
        toProduce.release(buffer_size);
        ui->progressBar->setMaximum(buffer_size);
        ui->pushButton->setEnabled(true);
        ui->pushButton_2->setEnabled(true);
        ui->Stop_Threads->setEnabled(true);
    
}

}
void Widget::o
...
...
(Please download the complete source code to view)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
Consumer.h259.00 B10-04-14|18:56
D_PV.pro401.00 B09-04-14|23:07
D_PV.pro.user12.99 kB10-04-14|21:01
main.cpp175.00 B08-04-14|14:49
Producer.h263.00 B10-04-14|18:56
widget.cpp4.35 kB10-04-14|21:01
widget.h688.00 B10-04-14|19:00
widget.ui3.41 kB10-04-14|18:59
D_PV0.00 B10-04-14|21:01
...
Sponsored links

widget.cpp (5.64 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D