Results 1 to 20 of 20

Thread: Sample for Mutithreading

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2010
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Sample for Mutithreading

    Hi,

    I try to implement a kind of datalogger to read the data from the inverters of my solar plant. The communication is done be serial port.

    I decided to make it multithreaded. I'm a C# and Java professional but C++ is driving me crazy :-(

    I stuck in a very simple problem. I want to create an overall handling object that will be part of the main program. It shall handle 1 database thread and multiple (number not fixed) threads for serial port handling. The serial threads shall emit signals to enqueue their data into a queue which will be written to a SQLITE database by the database thread.

    How is the best structure to manage the running serial threads? I tried

    private:
    QVector<SerialThread> _serialThreads;

    While building the solution it tells on a _serials.resize(1) line, that QObject::QObject(&QObject) is private

    I have no clue how to handle these threads in the best way.

    Any help will be appreciated.

    Thanks in advance
    HugoHiasl

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sample for Mutithreading

    Changing to :
    Qt Code:
    1. QVector<*SerialThread> _serialThreads;
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2010
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Sample for Mutithreading

    Quote Originally Posted by Lesiok View Post
    Changing to :
    Qt Code:
    1. QVector<*SerialThread> _serialThreads;
    To copy to clipboard, switch view to plain text mode 
    This leads to a build error: '*' cannot appear in constant expression


    This is my main handling object header:

    Qt Code:
    1. #ifndef YADLOBJECT_H
    2. #define YADLOBJECT_H
    3.  
    4. #include <QObject>
    5. #include <QVector>
    6. #include "Serial/serialthread.h"
    7. #include "Database/dbthread.h"
    8.  
    9. class YADLObject : public QObject
    10. {
    11. Q_OBJECT
    12. public:
    13. YADLObject();
    14. ~YADLObject();
    15.  
    16. private:
    17. QVector<*SerialThread> _serialThreads;
    18.  
    19. DBThread _dbThread;
    20.  
    21. public slots:
    22. void initialize();
    23. };
    24.  
    25. #endif // YADLOBJECT_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 12th June 2010 at 17:19. Reason: missing [code] tags

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Sample for Mutithreading

    That was a typo by Lesiok

    SerialThread*

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Sample for Mutithreading

    Quote Originally Posted by HugoHiasl View Post
    and multiple (number not fixed) threads for serial port handling.
    How many serial ports are you reading from?
    If the answer is one, you are now professionally shooting your foot, your other one and your head off.

    Keep it simple

  6. #6
    Join Date
    Jun 2010
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Sample for Mutithreading

    Quote Originally Posted by tbscope View Post
    How many serial ports are you reading from?
    If the answer is one, you are now professionally shooting your foot, your other one and your head off.

    Keep it simple
    The micro2440 board has 3 serials on boards and it has multiple USB-Ports which could be used with a converter. I think the minimal / normal usage is reading from 1 port. But for my solar plant there are 2 different manufacturers of inverters with different protocols. So I need at least 2 different threads for serial reading.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sample for Mutithreading

    Quote Originally Posted by HugoHiasl View Post
    So I need at least 2 different threads for serial reading.
    No, you don't. You only need one thread. If you use two, you are entering the "shoot yourself in the foot" world. Unless of course you need real concurrency when reading the ports but I'm almost sure you don't.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jun 2010
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Sample for Mutithreading

    Quote Originally Posted by wysota View Post
    No, you don't. You only need one thread. If you use two, you are entering the "shoot yourself in the foot" world. Unless of course you need real concurrency when reading the ports but I'm almost sure you don't.
    First of all thanks for the tip with the QVector. This worked for me :-)

    What you are telling is an interesting point. I thought using multiple threads would be easier and necessary because the handling of the different manufacturers on the different ports is really completely different. The first one has a RS485 protokoll that must be parsed by myself. I also need to lock the communiaction on this serial port until I got response for my query. The other manufacturer delivers a shared object with an API to query the data with. Here I do not have any direct influence how long the communication on this serial port is blocked and if probably the thread is blocked too.

    Others setups might have RS485 protocol with polling on serial port 1 and RS232 protocol with getting every 10 seconds data automatically pushed by the inverter on serial port 2.

    To be open for those other setups I thought it would be less complicated to handle it in single threads per port. But probably your right and I should use only one and write an handling object for every port and call them sequentially in a loop to query the data from the ports.

    I need to think about it.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sample for Mutithreading

    If all communication with the port is done asynchronously then you only need one thread for everything. You don't need to loop over ports all the time to query them for data, both serial port implementations for Qt should provide the readyRead() signal for the serial device so you will get notified when there is anything to read.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Jun 2010
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Sample for Mutithreading

    Hmm.... thanks for this really interesting approach. I will do some tests with a single thread environment.

    Actually I stuck in another problem. As mentioned earlier I'm really familiar with C# and Java. But I'm completely new to QT. I plan to create a BaseClass for the handlers for different manufacturers.

    I planned to make a base class that defines the set of methods that will be called by the serial thread. First I tried to make an Interface but I didn't manage it The implementation classes shall emit Signals to be able to connect them to the database thread.

    Now I'm trying it with a BaseClass which I want to subclass. But even with my 10 line trial I stuck in linker warnings.

    This is the implmentation now:

    serialhandlerbase.h

    Qt Code:
    1. #ifndef SERIALHANDLERBASE_H
    2. #define SERIALHANDLERBASE_H
    3.  
    4. #include <QObject>
    5.  
    6. class SerialHandlerBase : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. SerialHandlerBase();
    11. virtual void startHandling();
    12. virtual void stopHandling();
    13. };
    14.  
    15. #endif // SERIALHANDLERBASE_H
    To copy to clipboard, switch view to plain text mode 


    serialbasehandler.cpp

    Qt Code:
    1. #include "serialhandlerbase.h"
    2.  
    3. SerialHandlerBase::SerialHandlerBase()
    4. {
    5. }
    To copy to clipboard, switch view to plain text mode 


    The compiler says: undefined reference to 'vtable for SerialHandlerBase'
    and the linker: collect2: Id returned 1 exit status

    I can get rid of the compiler messages if I declare a destructor and implement it in the cpp file. But it does not fix the linker message.

    Do you have an idea for me?
    Last edited by wysota; 13th June 2010 at 12:34. Reason: missing [code] tags

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sample for Mutithreading

    If you don't want to implement the virtual methods in the class (making it an interface or a pure abstract class as we call it in C++), you need to tell that to the compiler:
    Qt Code:
    1. #ifndef SERIALHANDLERBASE_H
    2. #define SERIALHANDLERBASE_H
    3.  
    4. #include <QObject>
    5.  
    6. class SerialHandlerBase : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. SerialHandlerBase();
    11. virtual void startHandling() = 0;
    12. virtual void stopHandling() = 0;
    13. };
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Jun 2010
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Sample for Mutithreading

    I tried adding these = 0 but it did not change anything

    Qt Code:
    1. #ifndef SERIALHANDLERBASE_H
    2. #define SERIALHANDLERBASE_H
    3.  
    4. #include <QObject>
    5.  
    6. class SerialHandlerBase : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. SerialHandlerBase();
    11. virtual void startHandling() = 0;
    12. virtual void stopHandling() = 0;
    13. };
    14.  
    15. #endif // SERIALHANDLERBASE_H
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sample for Mutithreading

    Did you run qmake after adding the Q_OBJECT macro to your class?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Jun 2010
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Sample for Mutithreading

    Quote Originally Posted by wysota View Post
    Did you run qmake after adding the Q_OBJECT macro to your class?
    This was the reason. Perfect thanks :-)

    I'll try to implement the handler class now. Am I able to set the owner thread to sleep ? I think my next question will be concerning connecting the signals of the objects of the serial thread to the slots of the object in the database thread.

    But berfore asking this I'll do some trials on my own.

    Thank you very much for your patience.

Similar Threads

  1. UTF-8 Issues in Sample
    By trevelyan in forum Newbie
    Replies: 6
    Last Post: 19th May 2011, 01:41
  2. Problem with QSettings sample
    By neoclaw in forum Qt Programming
    Replies: 3
    Last Post: 3rd June 2010, 09:52
  3. Question regarding the flicklist qt sample
    By advokate3 in forum Qt Programming
    Replies: 0
    Last Post: 8th December 2009, 20:15
  4. mysql connection sample
    By mohanakrishnan in forum Newbie
    Replies: 3
    Last Post: 12th November 2009, 16:23
  5. Trouble building a sample (4.6)
    By Asperamanca in forum Newbie
    Replies: 12
    Last Post: 21st October 2009, 08:55

Tags for this Thread

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.