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. thread variables sharing problem between two slots
Forum Updated to NodeBB v4.3 + New Features

thread variables sharing problem between two slots

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 3.0k Views 2 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.
  • N Offline
    N Offline
    Nimika
    wrote on last edited by
    #1

    Hello,
    I am using the Qthread using Qobject and connecting the two threads (which are running simultaneously) using signal and slot but the variables of both the threads are not sharing.
    As the updated variables of one thread are being used in another thread.
    Kindly help me out!

    raven-worxR 1 Reply Last reply
    0
    • N Nimika

      Hello,
      I am using the Qthread using Qobject and connecting the two threads (which are running simultaneously) using signal and slot but the variables of both the threads are not sharing.
      As the updated variables of one thread are being used in another thread.
      Kindly help me out!

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Nimika said in thread variables sharing problem between two slots:

      Kindly help me out!

      i can't see any question in your post, only descriptions/facts?!

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      N 2 Replies Last reply
      0
      • raven-worxR raven-worx

        @Nimika said in thread variables sharing problem between two slots:

        Kindly help me out!

        i can't see any question in your post, only descriptions/facts?!

        N Offline
        N Offline
        Nimika
        wrote on last edited by
        #3

        @raven-worx question is about I am having problem in sharing the variables of different threads.

        1 Reply Last reply
        0
        • raven-worxR raven-worx

          @Nimika said in thread variables sharing problem between two slots:

          Kindly help me out!

          i can't see any question in your post, only descriptions/facts?!

          N Offline
          N Offline
          Nimika
          wrote on last edited by
          #4

          @raven-worx As suppose I am using variable X as 0 in thread 1 which got updated in second instant and I want to use the updated value of X in another thread 2, then it is showing a 0 value not the updated means updated value can be seen in thread 1 but not in thread 2.

          jsulmJ 1 Reply Last reply
          0
          • N Nimika

            @raven-worx As suppose I am using variable X as 0 in thread 1 which got updated in second instant and I want to use the updated value of X in another thread 2, then it is showing a 0 value not the updated means updated value can be seen in thread 1 but not in thread 2.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Nimika How exactly do you share the variable? Do you use same worker class in both threads? It's really unclear what you are actually doing. Best would be you post the code here.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            N 1 Reply Last reply
            1
            • jsulmJ jsulm

              @Nimika How exactly do you share the variable? Do you use same worker class in both threads? It's really unclear what you are actually doing. Best would be you post the code here.

              N Offline
              N Offline
              Nimika
              wrote on last edited by
              #6

              @jsulm Hello this is a small version of my program which I shared below:
              Can anyone give any idea about my problem by this code?
              void SerialInterface::RS5_function(QThread & RS5Thread)
              {
              connect(&RS5Thread,SIGNAL(started()),this,SLOT(RS5_slot()),Qt::DirectConnection);
              }
              void SerialInterface::RS6_function(QThread & RS6Thread)
              {
              connect(&RS6Thread,SIGNAL(started()),this,SLOT(RS6_slot()),Qt::DirectConnection);
              }

              void SerialInterface::RS5_slot()
              {

              serialPort1.setPortName("ttyM0");
              serialPort1.setBaudRate(QSerialPort:: Baud19200); // Setting BaudRate
              serialPort1.setDataBits(QSerialPort:: Data8); // Setting Data Bits
              serialPort1.setParity(QSerialPort:: EvenParity); // Setting Parity bit
              serialPort1.setStopBits(QSerialPort:: OneStop); // Setting Stop bit

              if (serialPort1.open(QIODevice::ReadOnly)) {
              
                  qDebug()<<"COM port ttyM0 has been connected" <<endl;
              
              } else
                  qDebug()<< "Open error"<< endl;
              

              QByteArray readData;
              if(serialPort1.waitForReadyRead(1500))
              readData= serialPort1.readAll();
              QByteArray t1;
              qDebug()<<"Recvd data of RS5"<< readData.toHex();
              decodeS40XP10Hz((unsigned char*)readData.data());

              }
              void SerialInterface::RS6_slot()
              {

              serialPort2.setPortName("ttyM1");
              serialPort2.setBaudRate(QSerialPort:: Baud115200);                                                      // Setting BaudRate
              serialPort2.setDataBits(QSerialPort:: Data8);                                                          // Setting Data Bits
              serialPort2.setParity(QSerialPort:: EvenParity);                                                       // Setting Parity bit
              serialPort2.setStopBits(QSerialPort:: OneStop);                                                        // Setting Stop bit
              
              if (serialPort2.open(QIODevice::ReadOnly)) {
              
                  qDebug()<<"COM port ttyM1 has been connected" <<endl;
              
              } else
                  qDebug()<< "Open error"<< endl;
              

              QByteArray readData;
              if(serialPort2.waitForReadyRead(1500))
              readData = serialPort2.readAll();
              QByteArray readData;
              qDebug()<<"Recvd data of RS6 "<< readData.toHex();
              decodeS40XP100Hz((unsigned char*)readData.data());
              }

              I have two slots named RS 5 and RS 6 which are in SerialInterface class and using the function decodeS40XP100Hz in one thread and decodeS40XP10Hz in another thread and whose updated variables are used in each other thread which are not getting updated.

              jsulmJ 1 Reply Last reply
              0
              • N Nimika

                @jsulm Hello this is a small version of my program which I shared below:
                Can anyone give any idea about my problem by this code?
                void SerialInterface::RS5_function(QThread & RS5Thread)
                {
                connect(&RS5Thread,SIGNAL(started()),this,SLOT(RS5_slot()),Qt::DirectConnection);
                }
                void SerialInterface::RS6_function(QThread & RS6Thread)
                {
                connect(&RS6Thread,SIGNAL(started()),this,SLOT(RS6_slot()),Qt::DirectConnection);
                }

                void SerialInterface::RS5_slot()
                {

                serialPort1.setPortName("ttyM0");
                serialPort1.setBaudRate(QSerialPort:: Baud19200); // Setting BaudRate
                serialPort1.setDataBits(QSerialPort:: Data8); // Setting Data Bits
                serialPort1.setParity(QSerialPort:: EvenParity); // Setting Parity bit
                serialPort1.setStopBits(QSerialPort:: OneStop); // Setting Stop bit

                if (serialPort1.open(QIODevice::ReadOnly)) {
                
                    qDebug()<<"COM port ttyM0 has been connected" <<endl;
                
                } else
                    qDebug()<< "Open error"<< endl;
                

                QByteArray readData;
                if(serialPort1.waitForReadyRead(1500))
                readData= serialPort1.readAll();
                QByteArray t1;
                qDebug()<<"Recvd data of RS5"<< readData.toHex();
                decodeS40XP10Hz((unsigned char*)readData.data());

                }
                void SerialInterface::RS6_slot()
                {

                serialPort2.setPortName("ttyM1");
                serialPort2.setBaudRate(QSerialPort:: Baud115200);                                                      // Setting BaudRate
                serialPort2.setDataBits(QSerialPort:: Data8);                                                          // Setting Data Bits
                serialPort2.setParity(QSerialPort:: EvenParity);                                                       // Setting Parity bit
                serialPort2.setStopBits(QSerialPort:: OneStop);                                                        // Setting Stop bit
                
                if (serialPort2.open(QIODevice::ReadOnly)) {
                
                    qDebug()<<"COM port ttyM1 has been connected" <<endl;
                
                } else
                    qDebug()<< "Open error"<< endl;
                

                QByteArray readData;
                if(serialPort2.waitForReadyRead(1500))
                readData = serialPort2.readAll();
                QByteArray readData;
                qDebug()<<"Recvd data of RS6 "<< readData.toHex();
                decodeS40XP100Hz((unsigned char*)readData.data());
                }

                I have two slots named RS 5 and RS 6 which are in SerialInterface class and using the function decodeS40XP100Hz in one thread and decodeS40XP10Hz in another thread and whose updated variables are used in each other thread which are not getting updated.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Nimika I would like first to ask why you need threads? You can handle serial communication with Qt without any threads.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                N 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @Nimika I would like first to ask why you need threads? You can handle serial communication with Qt without any threads.

                  N Offline
                  N Offline
                  Nimika
                  wrote on last edited by
                  #8

                  @jsulm This is only a part of my program means I have a lot connection which needs threading so I have to use it.

                  jsulmJ 1 Reply Last reply
                  0
                  • N Nimika

                    @jsulm This is only a part of my program means I have a lot connection which needs threading so I have to use it.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Nimika

                    void SerialInterface::RS5_function(QThread & RS5Thread)
                    {
                    connect(&RS5Thread,SIGNAL(started()),this,SLOT(RS5_slot()),Qt::DirectConnection);
                    }
                    

                    why do you start RS5_slot() when the thread starts and in which thread is it supposed to be executed?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    N 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @Nimika

                      void SerialInterface::RS5_function(QThread & RS5Thread)
                      {
                      connect(&RS5Thread,SIGNAL(started()),this,SLOT(RS5_slot()),Qt::DirectConnection);
                      }
                      

                      why do you start RS5_slot() when the thread starts and in which thread is it supposed to be executed?

                      N Offline
                      N Offline
                      Nimika
                      wrote on last edited by
                      #10

                      @jsulm I have to use many serial ports in RS 5 and many serial ports in RS 6 slots which have to run simultaneously thats why I am using threads and the variables of the thread are used by those decode functions and those variables are further used by another class.

                      jsulmJ 1 Reply Last reply
                      0
                      • N Nimika

                        @jsulm I have to use many serial ports in RS 5 and many serial ports in RS 6 slots which have to run simultaneously thats why I am using threads and the variables of the thread are used by those decode functions and those variables are further used by another class.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Nimika If you did not move SerialInterface instance to RS5Thread then RS5_slot() will not be executed in that thread, that's what I wanted to say:

                        connect(&RS5Thread,SIGNAL(started()),this,SLOT(RS5_slot()),Qt::DirectConnection);
                        

                        And you can use several serial port interfaces without threads in Qt as Qt is an asynchronous framework...
                        This is a good description: https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        N 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @Nimika If you did not move SerialInterface instance to RS5Thread then RS5_slot() will not be executed in that thread, that's what I wanted to say:

                          connect(&RS5Thread,SIGNAL(started()),this,SLOT(RS5_slot()),Qt::DirectConnection);
                          

                          And you can use several serial port interfaces without threads in Qt as Qt is an asynchronous framework...
                          This is a good description: https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

                          N Offline
                          N Offline
                          Nimika
                          wrote on last edited by
                          #12

                          @jsulm Ok let me try out. Thank you!

                          1 Reply Last reply
                          0

                          • Login

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