Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QtConcurrent::run() with virtual function
Forum Updated to NodeBB v4.3 + New Features

QtConcurrent::run() with virtual function

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 282 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mihan
    wrote on 2 Aug 2021, 08:33 last edited by
    #1

    Hi all

    I want to make a abstract class to process data from different IO devices.

    I have an idea as follow:

    class AbstractCommunication : public QObject
    {
        Q_OBJECT
    public:
        QMutex m_CacheMutex;       
        QByteArray m_receiveCache;  
    
    public:
        explicit AbstractCommunication(QIODevice *device);
        void sendData(QByteArray data);
    
        int receiveTick() const;
    
    private:
        virtual void processPacket();
    
    private slots:
        void receiveData();
    
    private:
        QIODevice *m_communication;
        int m_receiveTick;         
    };
    
    AbstractCommunication::AbstractCommunication(QIODevice *device) :
        m_communication(device)
    {
        m_receiveCache.clear();
        if(m_communication->isOpen())
        {
            connect(m_communication,SIGNAL(readyRead()),this,SLOT(receiveData()));
            QtConcurrent::run(this,&AbstractCommunication::processPacket);
        }
        else
        {
            qDebug()<<"device is not ready";
        }
    }
    
    void AbstractCommunication::sendData(QByteArray data)
    {
        m_communication->write(data);
    }
    
    void AbstractCommunication::receiveData()
    {
        QMutexLocker lock(&m_CacheMutex);
        
        m_receiveCache.append(m_communication->readAll());
        
        m_receiveTick = (m_receiveTick + 1) % MAX_RECEIVE_TICK;  
    
        // then process the data with the same protocol
        
        
    }
    
    nt AbstractCommunication::receiveTick() const
    {
        return m_receiveTick;
    }
    
    // this virtual function will be overwrite in different module, which inherit this class.
    void AbstractCommunication::processPacket()
    {
        qDebug()<<"in virtual thread".
    }
    

    first it doesn't work. then I improve the max count of thread pool QThreadPool::globalInstance()->setMaxThreadCount(8);,
    sometimes it works, but sometimes it only print "in virtual thread".'

    does the pointer this mean this abstract class , not the class inheriting it?

    Best regards
    Mihan

    K 1 Reply Last reply 2 Aug 2021, 08:50
    0
    • V Offline
      V Offline
      VRonin
      wrote on 2 Aug 2021, 08:47 last edited by
      #2

      I think it's more the fact that you are passing a pointer to the base class method. Try:
      QtConcurrent::run([this](){processPacket();});

      P.S.
      Using QtConcurrent here is unsafe, this could get deleted/go out of scope while processPacket() is still running

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • M Mihan
        2 Aug 2021, 08:33

        Hi all

        I want to make a abstract class to process data from different IO devices.

        I have an idea as follow:

        class AbstractCommunication : public QObject
        {
            Q_OBJECT
        public:
            QMutex m_CacheMutex;       
            QByteArray m_receiveCache;  
        
        public:
            explicit AbstractCommunication(QIODevice *device);
            void sendData(QByteArray data);
        
            int receiveTick() const;
        
        private:
            virtual void processPacket();
        
        private slots:
            void receiveData();
        
        private:
            QIODevice *m_communication;
            int m_receiveTick;         
        };
        
        AbstractCommunication::AbstractCommunication(QIODevice *device) :
            m_communication(device)
        {
            m_receiveCache.clear();
            if(m_communication->isOpen())
            {
                connect(m_communication,SIGNAL(readyRead()),this,SLOT(receiveData()));
                QtConcurrent::run(this,&AbstractCommunication::processPacket);
            }
            else
            {
                qDebug()<<"device is not ready";
            }
        }
        
        void AbstractCommunication::sendData(QByteArray data)
        {
            m_communication->write(data);
        }
        
        void AbstractCommunication::receiveData()
        {
            QMutexLocker lock(&m_CacheMutex);
            
            m_receiveCache.append(m_communication->readAll());
            
            m_receiveTick = (m_receiveTick + 1) % MAX_RECEIVE_TICK;  
        
            // then process the data with the same protocol
            
            
        }
        
        nt AbstractCommunication::receiveTick() const
        {
            return m_receiveTick;
        }
        
        // this virtual function will be overwrite in different module, which inherit this class.
        void AbstractCommunication::processPacket()
        {
            qDebug()<<"in virtual thread".
        }
        

        first it doesn't work. then I improve the max count of thread pool QThreadPool::globalInstance()->setMaxThreadCount(8);,
        sometimes it works, but sometimes it only print "in virtual thread".'

        does the pointer this mean this abstract class , not the class inheriting it?

        Best regards
        Mihan

        K Offline
        K Offline
        KroMignon
        wrote on 2 Aug 2021, 08:50 last edited by
        #3

        @Mihan said in QtConcurrent::run() with virtual function:

        QtConcurrent::run(this,&AbstractCommunication::processPacket);

        Why do you do this in base class constructor?
        This will call AbstractCommunication::processPacket() and not any overload... So no need to made it virtual!

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        1 Reply Last reply
        3
        • M Offline
          M Offline
          Mihan
          wrote on 3 Aug 2021, 01:37 last edited by
          #4

          Sorry for late and thanks a lot @VRonin @KroMignon

          I modified it according to your suggestions, now it works well!

          1 Reply Last reply
          0

          1/4

          2 Aug 2021, 08:33

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved