AndresBarbaRoja
9th March 2011, 17:24
Hi, I am trying to solve a problem in which I have a producer receiving messages and storing them into a buffer without max limit. The consumer checks if there are any new messages and if any, it will create a new thread to process it.
I'm almost a complete newbie and I realize that I have a lot of C++ learning to do (and i'm doing it) but i have a dead line and need some urgent help.
at the moment the first problem that I encounter is that I do not know where to store the QSemaphore. In the examples they are declared as global variables, but will not have a main for my module. Right now i'm declaring it as a static public member of the consumer class so the producer can have access to it knowing only the class (i am not sure how to pass a consumer object's member to the producer object).
The second problem that i'm having is that my program compiles but does not link and i do not know where is the problem :S
These are my classes.
The consumer's .h:
#ifndef ETHTHREAD_H
#define ETHTHREAD_H
#include <QThread>
#include <QSemaphore>
#include <QObject>
#include <QString>
#include <iostream>
#include <QQueue>
using namespace std;
class EthThread : public QThread
{
Q_OBJECT
public:
EthThread(QObject *parent = 0);
void run();
static void enqueuePFLPMessage(QString message);
static QString retrievePFLPMessage();
static QSemaphore PFLPMessagesInQueue;
private:
static QQueue<QString> PFLPQueue;
};
#endif // ETHTHREAD_H
The consumers implementation.
#include "eththread.h"
#include "pflp_linkmanager.h"
EthThread::EthThread(QObject *parent) :
QThread(parent){}
void EthThread::run()
{
PFLP_LinkManager producer(new PFLP_LinkManager);
producer.start();
while(1)
{
sleep(3);
if(PFLPMessagesInQueue.tryAcquire())
cout<<retrievePFLPMessage().data();
else
cout<<"EmptyQueue";
}
exec();
}
void EthThread::enqueuePFLPMessage(QString message)
{
PFLPQueue.enqueue(message);
}
QString EthThread::retrievePFLPMessage()
{
return PFLPQueue.dequeue();
}
The producer's heading:
#ifndef PFLP_LINKMANAGER_H
#define PFLP_LINKMANAGER_H
#include <QThread>
#include <QObject>
using namespace std;
class PFLP_LinkManager : public QThread
{
Q_OBJECT
public:
PFLP_LinkManager(QObject *parent = 0);
void run();
};
#endif // PFLP_LINKMANAGER_H
and implementation:
#include "pflp_linkmanager.h"
#include "eththread.h"
PFLP_LinkManager::PFLP_LinkManager(QObject *parent) :
QThread(parent)
{
}
void PFLP_LinkManager::run()
{
while(1)
{
sleep(5);
EthThread::enqueuePFLPMessage("MensajePFLP");
EthThread::PFLPMessagesInQueue.release();
}
exec();
}
And finally the main:
#include <QtCore/QCoreApplication>
#include <QString>
#include <eththread.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
EthThread module;
module.start();
return a.exec();
}
Thanks in advance for any help
I'm almost a complete newbie and I realize that I have a lot of C++ learning to do (and i'm doing it) but i have a dead line and need some urgent help.
at the moment the first problem that I encounter is that I do not know where to store the QSemaphore. In the examples they are declared as global variables, but will not have a main for my module. Right now i'm declaring it as a static public member of the consumer class so the producer can have access to it knowing only the class (i am not sure how to pass a consumer object's member to the producer object).
The second problem that i'm having is that my program compiles but does not link and i do not know where is the problem :S
These are my classes.
The consumer's .h:
#ifndef ETHTHREAD_H
#define ETHTHREAD_H
#include <QThread>
#include <QSemaphore>
#include <QObject>
#include <QString>
#include <iostream>
#include <QQueue>
using namespace std;
class EthThread : public QThread
{
Q_OBJECT
public:
EthThread(QObject *parent = 0);
void run();
static void enqueuePFLPMessage(QString message);
static QString retrievePFLPMessage();
static QSemaphore PFLPMessagesInQueue;
private:
static QQueue<QString> PFLPQueue;
};
#endif // ETHTHREAD_H
The consumers implementation.
#include "eththread.h"
#include "pflp_linkmanager.h"
EthThread::EthThread(QObject *parent) :
QThread(parent){}
void EthThread::run()
{
PFLP_LinkManager producer(new PFLP_LinkManager);
producer.start();
while(1)
{
sleep(3);
if(PFLPMessagesInQueue.tryAcquire())
cout<<retrievePFLPMessage().data();
else
cout<<"EmptyQueue";
}
exec();
}
void EthThread::enqueuePFLPMessage(QString message)
{
PFLPQueue.enqueue(message);
}
QString EthThread::retrievePFLPMessage()
{
return PFLPQueue.dequeue();
}
The producer's heading:
#ifndef PFLP_LINKMANAGER_H
#define PFLP_LINKMANAGER_H
#include <QThread>
#include <QObject>
using namespace std;
class PFLP_LinkManager : public QThread
{
Q_OBJECT
public:
PFLP_LinkManager(QObject *parent = 0);
void run();
};
#endif // PFLP_LINKMANAGER_H
and implementation:
#include "pflp_linkmanager.h"
#include "eththread.h"
PFLP_LinkManager::PFLP_LinkManager(QObject *parent) :
QThread(parent)
{
}
void PFLP_LinkManager::run()
{
while(1)
{
sleep(5);
EthThread::enqueuePFLPMessage("MensajePFLP");
EthThread::PFLPMessagesInQueue.release();
}
exec();
}
And finally the main:
#include <QtCore/QCoreApplication>
#include <QString>
#include <eththread.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
EthThread module;
module.start();
return a.exec();
}
Thanks in advance for any help