What makes something not thread safe?
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?
Re: What makes something not thread safe?
Quote:
Originally Posted by
tgreaves
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.
Quote:
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.
Re: What makes something not thread safe?
Quote:
Originally Posted by
wysota
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?
Re: What makes something not thread safe?
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.
Re: What makes something not thread safe?
Quote:
Originally Posted by
wysota
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?
Re: What makes something not thread safe?
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?
Re: What makes something not thread safe?
Re: What makes something not thread safe?
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..
Re: What makes something not thread safe?
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".
Re: What makes something not thread safe?
Quote:
Originally Posted by
wysota
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: