PDA

View Full Version : Producer Consumer...



Shuchi Agrawal
15th February 2007, 11:57
Hi,
In my program I have a form and 2 edittext boxes (say e1 and e2) on it.
Now I want tht when I write on e1 then the thread1 shd start and write the content in a buffer( byte array) and thread2 read the buffer only if its written by thread1 and write it to e2.
So now I cant access widgets fm thread classes so I need to transfer data to and fro the producer consumer classes to form class too so as to write on it.
So I want to ask tht when to start when to wait the threads in this case. This is same as semaphore example in the doc but I m trying to implement in a diff manner and facing problem.

Producer::Producer(QObject *parent) : QThread(parent)
{ start(); wait(); }

void Producer::get_str(QString str)
{ s = str; } // public slots : void get_str(QString str);

void Producer::run()
{ freeBytes.acquire(); usedBytes.release(); } // where freebytes and usedbytes are Qsemaphore global var

Consumer::Consumer(QObject *parent) : QThread(parent)
{ start(); wait(); }

void Consumer::run()
{ usedBytes.acquire(); freeBytes.release(); }


Form1::Form1(QWidget *parent1):QWidget(parent1)
{ ui.setupUi(this);
connect(ui.textEdit1,SIGNAL(textChanged()),this,SL OT(read_send()));
connect(ui.textEdit1,SIGNAL(textChanged()),this,SL OT(receive_write()));
}

void Form1::read_send()
{
QString str; str = ui.textEdit1->toPlainText(); producer_object.get_str(str); }

void Form1::receive_write()
{ ui.textEdit2->setText(s); }

here i have yet not implemented the code of using buffer and all. wil do it later.

Shuchi Agrawal
16th February 2007, 10:45
i have solved my problem. So dont reply.
thanks :)