Unsolved thread variables sharing problem between two slots
-
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! -
@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?!
-
@raven-worx question is about I am having problem in sharing the variables of different threads.
-
@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.
-
@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.
-
@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 bitif (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.
-
@Nimika I would like first to ask why you need threads? You can handle serial communication with Qt without any threads.
-
@jsulm This is only a part of my program means I have a lot connection which needs threading so I have to use it.
-
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?
-
@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.
-
@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/ -
@jsulm Ok let me try out. Thank you!