In the below program the Thread_write class writes data to a buffer every 100 milliseconds and the Thread_read reads the data from the buffer after the data has been written(this process will continue only 10 times).But even the timer is not started but the run function is executed ,where am i wrong.
#include <QtGui>
const int BufferSize = 10;
char buffer[BufferSize];
class Thread_write
: public QThread{
public:
void run()
{
startTimer(100);
}
{
for (int i=0; i<BufferSize; i++)
{
freeB.acquire();
buffer[i]=i;
usedB.release();
}
}
};
{
public:
void run()
{
for (int i=0; i<BufferSize; i++)
{
usedB.acquire();
qDebug()<<buffer[i];
freeB.release();
}
}
};
int main(int argc, char *argv[])
{
Thread_write tw;
tw.start();
Thread_read tr;
tr.start();
return a.exec();
}
#include <QtGui>
const int BufferSize = 10;
char buffer[BufferSize];
QSemaphore freeB(BufferSize);
QSemaphore usedB;
class Thread_write : public QThread
{
public:
void run()
{
startTimer(100);
}
void timerEvent(QTimerEvent *)
{
for (int i=0; i<BufferSize; i++)
{
freeB.acquire();
buffer[i]=i;
usedB.release();
}
}
};
class Thread_read : public QThread
{
public:
void run()
{
for (int i=0; i<BufferSize; i++)
{
usedB.acquire();
qDebug()<<buffer[i];
freeB.release();
}
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Thread_write tw;
tw.start();
Thread_read tr;
tr.start();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks