PDA

View Full Version : What makes something not thread safe?



tgreaves
18th February 2009, 17:41
I see that alot of Qt classes are not thread safe.. What makes them not thread safe?

Suppose I have a QPlainTextEdit box and I want to run a thread that will so some work and while its running will report back to the QPlainTextEdit box what its doing? Is this safe to do?

wysota
18th February 2009, 19:02
I see that alot of Qt classes are not thread safe.. What makes them not thread safe?
Static data and access to drawing resources of the underlying platform.


Suppose I have a QPlainTextEdit box and I want to run a thread that will so some work and while its running will report back to the QPlainTextEdit box what its doing? Is this safe to do?

If you use signals and slots or events to connect the thread and the widget then yes.

tgreaves
19th February 2009, 14:45
If you use signals and slots or events to connect the thread and the widget then yes.

What about if I pass a pointer to the widget to the thread and have it do a "widget*->setText()" there?

wysota
19th February 2009, 18:53
No, that's a guaranteed crash sooner or later. You can't access any of the widget methods not explicitely mentioned as thread-safe from worker threads.

tgreaves
20th February 2009, 15:33
If you use signals and slots or events to connect the thread and the widget then yes.

I see that there is a appendPlainText slot.. I always connected signals and slots together to do something.. How can I "run" a slot directly from the program(or thread)?

I know I can do widget->appendPlainText("abc") but how do I do that so its thread safe?

tgreaves
20th February 2009, 15:47
I think I might have it figured out.. Do I emit a signal in the thread and connect the signal from the thread to the plaintextedit's slot in the main program?

--- main program
connect(&thread, signal(addthis(QString)), ui.plainTextEdit1, slot(appendPlainText(QString));

--- thread

emit addthis("hello there");

Is this correct?

wysota
20th February 2009, 19:32
Yes, that's fine.

tgreaves
20th February 2009, 19:36
Great..

And suppose there is something "funky" that I want to do using this way.. If the class(plainTextEdit, QLineEdit, ect) doesnt have a slot for that, is it ok to create my own class that inherets from (plaintextedit, qlineedit, ect) and add a slot for the "funky" stuff that I want to do.. And then connect a signal to my new classes slot, and then emit the signal for that inside of the thread?

I dont see why I cant do that, Qt does it..

wysota
20th February 2009, 19:41
Yes, you can subclass and add your own signals and slots. Just remember it's probably not safe to transmit pointers across threads, so avoid such "funkiness".

tgreaves
20th February 2009, 20:16
Yes, you can subclass and add your own signals and slots. Just remember it's probably not safe to transmit pointers across threads, so avoid such "funkiness".

Well I wouldnt be transmitting a pointer across a thread.. I would be "looking" for a signal to be emitted by the thread and running a slot inside the main program/class.. :cool: