PDA

View Full Version : Thread safety



BalaQT
25th July 2011, 13:01
Dear experts,

I'm working in Qt4.7.4 in ubuntu. Does emitting the signal inside a thread is thread safety?

Ex:


class myThread::public QThread
{
.....
private:
void sendStatus(int num);
Signals:
void mySignal1();
void mySignal2();
};


void myThread::sendStatus(int num)
{
if(num)
{
emit mySignal1();
}
else
{
emit mySignal2();
}
}





class myClass::public QWidget
{
public:
........
........
myThread *m;
void myTrans1();
void myTrans2();

};

myClass()
{
m=new myThread();
connect(m,SIGNAL(mySignal1()),this,SLOT(myTrans1() );
connect(m,SIGNAL(mySignal2()),this,SLOT(myTrans2() );
}

void myClass::myTrans1()
{
//some code
}

void myClass::myTrans2()
{
//some code
}


is this thread safety?

Thanks,
Bala

MarekR22
26th July 2011, 10:16
No, if you do this like this!
QThread class is used to manipulate thread so if you add some functionality there not related to managing thread then you will accouter problems with thread safety.
See this article: You’re doing it wrong… (http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/)
Short: create QObject which should do something in thread, move it to thread, invoke slot which should do something, and then signals will be emitted from this slot in thread safe meaner.
See also QObject::moveToThread.

stampede
26th July 2011, 10:25
No wonder that people are "doing it wrong", thats what the docs says...

MarekR22
26th July 2011, 11:28
I take much of the blame for the confusion that comes with writing threaded Qt code. The original QThread class was abstract, so subclassing was necessary. It wasn’t until Qt 4.4 that QThread::run() gained a default implementation. (http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/)
I see in documentation inheritance example, but note that it doesn't provide extra signal slots for QThread (http://doc.qt.nokia.com/latest/qthread.html), it just creates some QObject in run method nothing else (this leads to a same thing as moveToThread). Anyway I agree that documentation needs update.

wysota
26th July 2011, 11:39
To add my five cents: it is thread-safe per se but it is probably not what you want to achieve.

BalaQT
26th July 2011, 14:27
So the right way is, as MarekR22 said,

1) create QObject which should do something
2) create a thread object
3) move the object to created thread
4) invoke slot which should do something
5) signals will be emitted from this slot

Do not inherit the QThread class unless if we need to add/override some features in it.

Pls correct me , if Im wrong.


@wysota:

To add my five cents: it is thread-safe per se but it is probably not what you want to achieve.
Master , you are saying that the orginal implementation is thread safe. But its not the proper way to do it. right?

Thanks for the excellent responses,
Cheers,
Bala

wysota
26th July 2011, 15:58
Yes, it is thread-safe because signal-slot mechanism assures that. But the slot executes in a different thread than you think so if it contains code that manipulates some data from a different thread, this manipulation will not be thread-safe unless the data itself assures such safety.