Results 1 to 2 of 2

Thread: Problems semaphores and buffers

  1. #1
    Join Date
    Mar 2011
    Posts
    23
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Problems semaphores and buffers

    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:
    Qt Code:
    1. #ifndef ETHTHREAD_H
    2. #define ETHTHREAD_H
    3.  
    4. #include <QThread>
    5. #include <QSemaphore>
    6. #include <QObject>
    7. #include <QString>
    8. #include <iostream>
    9. #include <QQueue>
    10. using namespace std;
    11.  
    12. class EthThread : public QThread
    13. {
    14. Q_OBJECT
    15. public:
    16. EthThread(QObject *parent = 0);
    17. void run();
    18. static void enqueuePFLPMessage(QString message);
    19. static QString retrievePFLPMessage();
    20. static QSemaphore PFLPMessagesInQueue;
    21. private:
    22. static QQueue<QString> PFLPQueue;
    23. };
    24.  
    25. #endif // ETHTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    The consumers implementation.
    Qt Code:
    1. #include "eththread.h"
    2. #include "pflp_linkmanager.h"
    3.  
    4. EthThread::EthThread(QObject *parent) :
    5. QThread(parent){}
    6.  
    7. void EthThread::run()
    8. {
    9. PFLP_LinkManager producer(new PFLP_LinkManager);
    10. producer.start();
    11.  
    12. while(1)
    13. {
    14. sleep(3);
    15. if(PFLPMessagesInQueue.tryAcquire())
    16. cout<<retrievePFLPMessage().data();
    17. else
    18. cout<<"EmptyQueue";
    19. }
    20. exec();
    21. }
    22.  
    23. void EthThread::enqueuePFLPMessage(QString message)
    24. {
    25. PFLPQueue.enqueue(message);
    26. }
    27.  
    28. QString EthThread::retrievePFLPMessage()
    29. {
    30. return PFLPQueue.dequeue();
    31. }
    To copy to clipboard, switch view to plain text mode 

    The producer's heading:
    Qt Code:
    1. #ifndef PFLP_LINKMANAGER_H
    2. #define PFLP_LINKMANAGER_H
    3.  
    4. #include <QThread>
    5. #include <QObject>
    6. using namespace std;
    7.  
    8. class PFLP_LinkManager : public QThread
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. PFLP_LinkManager(QObject *parent = 0);
    14.  
    15. void run();
    16. };
    17.  
    18. #endif // PFLP_LINKMANAGER_H
    To copy to clipboard, switch view to plain text mode 

    and implementation:
    Qt Code:
    1. #include "pflp_linkmanager.h"
    2. #include "eththread.h"
    3.  
    4. PFLP_LinkManager::PFLP_LinkManager(QObject *parent) :
    5. QThread(parent)
    6. {
    7. }
    8.  
    9. void PFLP_LinkManager::run()
    10. {
    11. while(1)
    12. {
    13. sleep(5);
    14. EthThread::enqueuePFLPMessage("MensajePFLP");
    15. EthThread::PFLPMessagesInQueue.release();
    16. }
    17. exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    And finally the main:
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QString>
    3. #include <eththread.h>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc, argv);
    8. EthThread module;
    9. module.start();
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance for any help

  2. #2
    Join Date
    Mar 2011
    Posts
    23
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problems semaphores and buffers

    Ok, I scrapped the code and now it runs and appears to be doing what I want, yet i do not know if there is any conceptual problem or hidden trap that i'm not triggering on my test.
    This solution is not the one i imagined first, because instead of a continous loop trying to acquire the the resources from the semaphore, i wanted to use signals to trigger the message management.
    I will post my code below, could anyone tell me if there is any problem at sight (of example I'm not sure if i'm using the mutex in the right way).

    main.cpp:
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <messagemanager.h>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7. MessageManager manager;
    8. manager.start();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    Consumer
    messagemanager.h:
    Qt Code:
    1. #ifndef MESSAGEMANAGER_H
    2. #define MESSAGEMANAGER_H
    3.  
    4. #include <QThread>
    5. #include <pflplinkmanager.h>
    6. #include <QDebug>
    7.  
    8. class MessageManager : public QThread
    9. {
    10. Q_OBJECT
    11. public:
    12. MessageManager(QObject *parent = 0);
    13.  
    14. protected:
    15. void run();
    16.  
    17. private:
    18. PFLPLinkManager pflpLink;
    19. };
    20.  
    21. #endif // MESSAGEMANAGER_H
    To copy to clipboard, switch view to plain text mode 

    messagemanager.cpp:
    Qt Code:
    1. #include "messagemanager.h"
    2.  
    3. MessageManager::MessageManager(QObject *parent) :
    4. QThread(parent)
    5. {
    6. }
    7.  
    8. void MessageManager::run()
    9. {
    10. forever
    11. {
    12. sleep(1);
    13. if(pflpLink.messagesInQueue.tryAcquire())
    14. qDebug()<<pflpLink.retrieveMessage();
    15. else
    16. qDebug()<<"No Messages";
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    Producer
    pflplinkmanager.h
    Qt Code:
    1. #ifndef PFLPLINKMANAGER_H
    2. #define PFLPLINKMANAGER_H
    3.  
    4. #include <QThread>
    5. #include <QMutex>
    6. #include <QQueue>
    7. #include <QString>
    8. #include <QSemaphore>
    9.  
    10. class PFLPLinkManager : public QThread
    11. {
    12. Q_OBJECT
    13. public:
    14. PFLPLinkManager(QObject *parent = 0);
    15. ~PFLPLinkManager();
    16.  
    17. void enqueueMessage(QString);
    18. QString retrieveMessage();
    19. QSemaphore messagesInQueue;
    20. protected:
    21. void run();
    22. private:
    23. QMutex mutex;
    24. bool abort;
    25. QQueue<QString> InMessagesQ;
    26. };
    27. #endif // PFLPLINKMANAGER_H
    To copy to clipboard, switch view to plain text mode 

    pflplinkmanager.cpp:
    Qt Code:
    1. #include "pflplinkmanager.h"
    2.  
    3. PFLPLinkManager::PFLPLinkManager(QObject *parent) :
    4. QThread(parent)
    5. {
    6. abort=false;
    7. start();
    8. }
    9.  
    10. PFLPLinkManager::~PFLPLinkManager()
    11. {
    12. mutex.lock();
    13. abort=true;
    14. mutex.unlock();
    15. wait();
    16. }
    17.  
    18. void PFLPLinkManager::run()
    19. {
    20. while(!abort)
    21. {
    22. sleep(5);//Cada 5 segundos voy a producir un mensaje
    23. QString message("Mensaje de entrada");
    24. enqueueMessage(message);
    25. messagesInQueue.release();
    26. }
    27.  
    28. }
    29.  
    30. void PFLPLinkManager::enqueueMessage(QString message)
    31. {
    32. QMutexLocker locker(&mutex);
    33. InMessagesQ.enqueue(message);
    34. }
    35.  
    36. QString PFLPLinkManager::retrieveMessage()
    37. {
    38. QMutexLocker locker(&mutex);
    39. return InMessagesQ.dequeue();
    40. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. The best way to use buffers / streams/ strstreams
    By tonnot in forum General Programming
    Replies: 0
    Last Post: 28th February 2011, 09:32
  2. About binary files & streams & buffers...
    By tonnot in forum General Programming
    Replies: 3
    Last Post: 22nd February 2011, 23:05
  3. Replies: 1
    Last Post: 25th November 2010, 16:02
  4. Dual frame buffers /dev/fb0 and /dev/fb1
    By ghassett in forum Qt Programming
    Replies: 0
    Last Post: 28th May 2009, 18:57
  5. Need help using multiple frame buffers
    By TTGator in forum Qt Programming
    Replies: 0
    Last Post: 20th November 2008, 17:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.